Functions 2: Instances

10 min

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

  1. Create a new part and rename it. This lesson will use FirePart.
  2. 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: FirePart, TrapPart

Create a New Script

  1. In the Explorer add a new script to FirePart.
  2. Rename the script. This lesson will use StopFire.
  3. Delete Hello World and add a comment describing the script’s purpose.
  4. Create a new variable named firePart that 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

  1. Under the comment, type local function stopFire()
  2. Press Enter to autocomplete the function with end.

Use Destroy to Stop Fires

With the function created, you can add instructions between stopFire() and end. You’ll use a pre-made function called Destroy() to remove the Fire that’s parented to firePart.

  1. Between stopFire() and end, type firePart.Fire:Destroy()

  2. Under end, call stopFire()

  3. 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

  1. Under firePart.Fire:Destroy(), type:
    local spark = Instance.new("ParticleEmitter")


  2. 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

  1. Run the game to see the fire get replaced with the ParticleEmitter!


Troubleshooting Tip
  • 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.

  1. 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.