PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Making an Explosion Course

Making an Explosion Course

Jul 02 2018, 4:08 PM PST 5 min

 

In this tutorial we will make an explosion-filled gauntlet using for loops and random numbers.

Building the Course

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

As shown in the image above, the most important feature of this course is that it’s filled with parts where we want explosions to occur. Each of these parts should be named ExplosionPart and be placed directly inside the Workspace.

Writing the Script

Now let’s add a script to make these parts explode.

First, insert a script into ServerScriptService. Adding an explosion via code is done with the same function we used to create a new part:

local 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:

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

This would center the explosion on one of the parts we inserted earlier.

This code creates just one explosion though, while we want all of the parts that we named ExplosionPart to explode. To do this, we will use 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 Instance/GetChildren|GetChildren() member function and store the result in a variable:

local children = game.Workspace:GetChildren()

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

local children = game.Workspace:GetChildren()
for _, child in ipairs(children) do
	local 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 Instance/GetChildren|GetChildren() returns all of the children in the workspace, including the Camera, Terrain, and any other parts that may be in our game.

Because of this, 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:

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

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:

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

Random Numbers

Right now all of our ExplosionPart parts will create an explosion on every loop. Let’s add a random element so that only some of the parts explode each loop.

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.

local function explodePart(part)
	local explosion = Instance.new("Explosion")
	explosion.Position = part.Position
	explosion.Parent = game.Workspace
end

local 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:

local number = math.random(1,3)

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

local function explodePart(part)
	local number = math.random(1,3)
	if number == 1 then
		local explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
	end
end

local 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

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 explodePart() function, we will add a new if statement, along with a new statement called else which is used to run logic if the first statement is not satisfied.

local function explodePart(part)
	if part.BrickColor == BrickColor.Red() then
		local explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
		part.BrickColor = BrickColor.White()
	else
		local 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 (which is the case by default), the function will generate a random number. If the random number equals 1 then we 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:

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

local 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

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

Tags:
  • coding
  • random