Functions 2: Instances
Functions 2: Instances
To practice functions, set fire to a part in the game editor, and then create a function that destroys the fire and turns it into sparkles at the start of the game.
Setting up the Part and Script
Create a New Part
- Create a new part and rename it. This lesson will use FirePart.
- Right-click FirePart, and under Effects, select Fire.
Naming Rules - Parts
Remember, parts use PascalCase where the first letter of each word is capitalized. For example:
Create a New Script
- In the Explorer add a new script to FirePart.
- Rename the script. This lesson will use StopFire.
Hello Worldand add a comment describing the script’s purpose.
- Create a new variable named
firePartthat refers to the script’s parent.
Destroying the Fire
A function that replaces fire with sparkles doesn’t exist in Roblox so you’ll create one named stopFire(). While you can name your function anything, this lesson will use
stopFire(). If you do create your own name, remember that function names should help you remember what they do.
Create a new Function
- Under the comment, type
local function stopFire()
- Press Enter to autocomplete the function with
Your code should now be:
Use Destroy to Stop Fires
With the function created, you can add instructions between
end. You’ll use a pre-made function called
Destroy() to remove the Fire that’s parented to
Between stopFire() and end, type f
Under end, call
Test your code. Your fire should disappear!
Creating new Instances
Anything added into a game like parts or scripts, are objects. Each copy of a particular type of object is called an instance. Fire is one type of instance. To replace the fire instance with sparkles, you’ll create an instance of the ParticleEmitter, which can be used to create sparkles, smoke, or other special effects.
Create a ParticleEmitter Instance
local spark = Instance.new("ParticleEmitter")
On the next line, add the ParticleEmitter to firePart by typing:
spark.Parent = firePart
By parenting the instance to FirePart, it’ll be positioned in exactly the same place.
Test the Script
Run the game to see the fire get replaced with the ParticleEmitter!
- Make sure that
"ParticleEmitter"is spelled exactly, has quotations, and is between
- Check that
stopFire()is written under end. If not, the function won’t be called, or told to start it’s instructions.
Finished Code Example:
Troubleshooting with Print Functions
Including a print function at different points in your code can help you find errors. For example, if you include a print function in the
stopFire() function, you can check to make sure the function is being called, even if the rest of your code didn’t work as intended.
- Add a print functions inside of your function that’ll help you check if the function was called.
These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.