Making an Explosion Course

This is an easy tutorial This is an easy tutorial. < Lighting Parts on Fire Making a Health Pickup > This tutorial is part of a series on scripting. Return to tutorial index

In the last tutorial we covered how to use while loops and conditions to make a day/night cycle and a streetlight. In this tutorial we will make an explosion-filled gauntlet using for loops and random numbers.

The first thing to do is to build the course the player will run through. Be sure to include a SpawnLocation

Scripting6 Gauntlet.png

The important feature of this course is that it is filled with parts where we want explosions to occur. Each of these parts are named "ExplosionPart" and are directly inside the workspace.

Scripting6 Explorer.png

Let's add a script to make these parts explode. Insert a script into ServerScriptService. Adding an explosion via code is done with the same function we used to create a new part:

explosion = Instance.new("Explosion")
explosion.Parent = game.Workspace

The thing about explosions is that we need to tell them where to explode. We can do this with an explosion's Position property. But where do we want the explosion? We could do something like this:

explosion = Instance.new("Explosion")
explosion.Position = game.Workspace.ExplosionPart.Positionexplosion.Parent = game.Workspace

This would center the explosion on one of the parts we inserted earlier. This code just creates one explosion though. We want all of the parts that we named ExplosionPart to explode. To do this, we will use a new kind of loop called a generic for loop. A generic for loop can be used in several ways, one of which is to cycle through a group of objects and do something to each one. In this tutorial we will use this technique to cycle through all of the children of Workspace to access each ExplosionPart.

First we need to get a list of all of the children in Workspace. We can do this with the GetChildren member function and store the result in a variable:

children = game.Workspace:GetChildren()

Now we can use a for loop to go through all of the children in that list.

children = game.Workspace:GetChildren()
for _, child in ipairs(children) do
	explosion = Instance.new("Explosion")
	explosion.Position = child.Position
	explosion.Parent = game.Workspace
end

Unfortunately, if we run this code, our game will create an error:

Scripting6 Error.png

This is because GetChildren returns all of the children in the Workspace. This includes the Camera, Terrain, and any other parts that may be in our game. We need to check if the child we are looking at is an ExplosionPart. We can do this with a conditional statement checking the name of the child:

children = game.Workspace:GetChildren()
for _, child in ipairs(children) do
	if child.Name == "ExplosionPart" then
		explosion = Instance.new("Explosion")
		explosion.Position = child.Position
		explosion.Parent = game.Workspace
	end
end
wait(1)

Now our code will run and will make an explosion at all of the correct places! Exploding only once is not very exciting though. Let's use a while true loop and a wait to make our code cycle so we get repeating explosions:

children = game.Workspace:GetChildren()
while true do
	for _, child in ipairs(children) do
		if child.Name == "ExplosionPart" then
			explosion = Instance.new("Explosion")
			explosion.Position = child.Position
			explosion.Parent = game.Workspace
		end
	end
	wait(1)
end

Random Numbers[edit]

Right now all of our ExplosionParts will create an explosion on every loop. Let's add a random element so that there is is only some of the parts explode. The first thing we should do is move our explosion code into a function as we will be adding more to it and we don't want the code in the loop to be hard to read.

function ExplodePart(part)
	explosion = Instance.new("Explosion")
	explosion.Position = part.Position
	explosion.Parent = game.Workspace
end
 
children = game.Workspace:GetChildren()
while true do
	for _, child in ipairs(children) do
		if child.Name == "ExplosionPart" then
			ExplodePart(child)
		end
	end
	wait(1)
end

The code will run no differently from before, the only difference is that it is better organized with the function. Now let's make a random number to determine if we want to make an explosion or not. Generating a random number is done with a special function:

number = math.random(1,3)

math.random will output a random number between the two numbers you provide between the parenthesis. In this case we are getting a random number between 1 and 3. We can use a conditional statement to check this number:

function ExplodePart(part)
	number = math.random(1,3)
	if number == 1 then
		explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
	end
end
 
children = game.Workspace:GetChildren()
while true do
	for _, child in ipairs(children) do
		if child.Name == "ExplosionPart" then
			ExplodePart(child)
		end
	end
	wait(1)
end

Warning Players[edit]

Right now there is no indication to a player that a given part is about to explode. Let's add some code that turns parts red before they explode.

In our handlePart function, we will add a new if statement, along with a new statement called else. Else is used to run logic if the first statement is not satisfied:

function ExplodePart(part)
	if part.BrickColor == BrickColor.Red() then
		explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
		part.BrickColor = BrickColor.White()
	else
		number = math.random(1,3)
		if number == 1 then
			part.BrickColor = BrickColor.Red()
		end
	end	
end

Let's walk through this function to understand what is going on now. The function first checks to see if the part has turned red. If it has, it cause an explosion and changes the part back to white. If the part is not red however (which is the case by default), the function will generate a random number. If the random number equals 1 then we will turn the part's color to red. This will cause the part to explode the next time it is called in this function.

The entire script should now look like this:

function ExplodePart(part)
	if part.BrickColor == BrickColor.Red() then
		explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
		part.BrickColor = BrickColor.White()
	else
		number = math.random(1,3)
		if number == 1 then
			part.BrickColor = BrickColor.Red()
		end
	end	
end
 
children = game.Workspace:GetChildren()
while true do
	for _, child in pairs(children) do
		if child.Name == "ExplosionPart" then
			ExplodePart(child)
		end
	end
	wait(1)
end

Now parts will randomly turn red and then explode, giving our player a chance to dodge out of the way!

This is an easy tutorial This is an easy tutorial. < Lighting Parts on Fire Making a Health Pickup > This tutorial is part of a series on scripting. Return to tutorial index