Setting Parts on Fire with Events
Setting Parts on Fire with Events
In this tutorial we will learn how to use events and functions to create areas in the game that light parts on fire and put them out.
The first thing is to put in two parts. One part will be used to set parts that hit it on fire, and the other part will put out fires. In the Explorer, make sure one is called FirePart and the other is called WaterPart.
We also need a
Script in the
ServerScriptService. Right click on the ServerScriptService, hover over Insert Object, and select Script.
We have been using functions quite a bit already: print and wait are functions that can work in any Roblox script, while SetMinutesAfterMidnight is a member function of the Lighting service. These functions have already been defined by Roblox, but we can create as many custom functions as we need to in our scripts.
First, we will need a function to light a part on fire. We will start the function like this:
There are several important parts to making a function. We have to start the line of code with the special keyword function. The next word will be the name of our function. The same rules for naming a variable applies to naming a function (no spaces, cannot start with a number, etc). Next, we add parentheses (). Then we add the code that we want to run when the function is called. In the above case, the code print(“Lighting on fire!”) will run when the function is called. After we put in all the code we want to run, we finish up with the keyword end.
Our function isn’t finished, but let’s first try calling our function to test it. You’ll notice that if you Run the game now, nothing will happen - the print statement that we wrote won’t show up in the Output. That is because while we said what we want the function to do, we never actually called it. We can call this function just like we would call other functions: Using the name of the function followed by parentheses:
Now if we run the game Lighting on fire! will show up in the Output.
When making your own functions, you must be sure that the function is defined earlier in the code than when you use it. The above example will work because we first said what the function is supposed to do, then we called it. If instead we wrote:
This code will not work. We call lightOnFire() on the first line, but since we hadn’t yet said what that function is supposed to do, the Script will error.
Let’s keep working on our function. Right now it only prints, and we want to make flames happen! We want to be able to use this function to light specific parts on fire, so next we need to make sure we can tell the function which part we want to light up. We do this by adding what is called a parameter to the function. In between the parentheses in the function definition, include the word part like this:
The word part in the above code is a parameter for our function. We have been using parameters in other functions: In the wait function we have to provide a number as a parameter and in the print function we can provide a string or a number as a parameter. In this case, we are going to provide the Part that we want to set on fire. Inside the function, we can use part just as we would use a normal variable.
Now let’s add some fire. The easiest way to do this is to create a new Fire instance in code, and make its parent the part that was passed in. Just like how we can create a new Part with the line Instance.new(“Part”), we can create fire with the line Instance.new(“Fire”). We have to make sure to store the fire in a variable so we can set its Parent property.
The last thing we need to do is call this function whenever a part hits the FirePart we created at the beginning of the tutorial. To do this, we are going to use something called an Event; Events are used to call functions when certain things happen in a game. Some examples of events are: when two things collide, when a player joins the game, and when a player presses a button; There are many places where we can call custom functions when events happen in the game. In this case, we are going to use an Event called Touched, which is associated with Parts:
Let’s test our game! Hit Play so we get a character and walk over the FirePart. Our character’s legs will catch on fire as soon as they touch it!
Putting out Fires
Let’s be nice to our players and add a way for them to put out the fire by stepping on the WaterPart. Just like lighting a fire, we need to create a function to put out fires and then connect that function to the WaterPart’s Touched event.
To get rid of fire we can use a function called Destroy. This function can be used on any object in Roblox to get rid of it. It is tempting to write the function just like this:
This code can put out fires, but what happens if we step on the WaterPart if our character isn’t on fire at all?
The above code creates an error! When coding, you want your game to not have any errors at all. To fix this, we first have to check if the part we are attempting to douse has any fire in it at all. This can be done easily with another function called FindFirstChild. This function is used to see if a named part is inside of something else. We can even store the result in a variable. Let’s do that now:
In our function, after the prints, we first create a variable called fire, and in it we store the result of FindFirstChild(“Fire”). The next line, if fire then, is a conditional check to see if anything was stored in fire. If FindFirstChild successfully stored something inside of the fire variable, then the next line, fire:Destroy() will be called. Otherwise, it won’t do anything because nothing was found. This way we don’t have any errors when we call the function.
Now we can test our game again with Play. We can now light our character on fire by stepping on the FirePart, and then douse the flames by stepping on the WaterPart.
Let’s combine our new code with some terrain! We can have a player light on fire if they step on lava, and they can put it out by jumping in water. This is much more interesting and immersive than simply stepping on blocks. First, lets add a bit of terrain using the Add terrain tool:
Move and scale the fire and water parts that we made earlier so that they completely overlap the terrain we just added. Make sure both parts are Anchored and that their
BasePart/CanCollide Property is off. You can also make them translucent with their Transparency properties to help position them.
Lastly, at the end of our script we will make the parts completely see-through so they don’t appear in the game:
firePart.Transparency = 1 waterPart.Transparency = 1
Now when we Play our game, we can jump onto the lava to light on fire and jump in the water to put it out!
Here is the code in full for what we just made: