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

Functions 2: Instances

10 min

Functions 2: Instances

Learning Objectives Students will be able to:
  • Create a function that replaces a fire instance with a ParticleEmitter instance.
Prerequisites Students should:

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.

-- Script removes the fire from firePart

-- Set up variables

local firePart = script.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.

Your code should now be:

-- Script removes the fire from firePart

-- Set up variables

local firePart = script.Parent

-- Create the function

local function stopFire()

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()
    functions2_fireScriptStep3.png

  2. Under end, call stopFire()
    functions2_fireScriptStep4.png

  3. Test your code. Your fire should disappear!
    deleteFireExample.gif

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.

creatingInstanceVariables.png

Create a ParticleEmitter Instance

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

    function2_fireScriptStep5.png

  2. On the next line, add the ParticleEmitter to firePart by typing:
    spark.Parent = firePart

    functions2_fireScriptStep6.png

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!

    fireToParticleExample.gif

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:

-- Replaces Fire with Sparkles

-- Set up variables

local firePart = script.Parent

-- Create the function

local function stopFire()

-- Destroy the fire instance

firePart.Fire:Destroy()

-- Add a new particle instance to the part

local spark = Instance.new("ParticleEmitter")

spark.Parent = firePart

end

stopFire()

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.

local function stopFire()

print("Starting stopFire function")

-- Destroy the fire instance

firePart.Fire:Destroy()

-- Add a new particle instance to the part

local spark = Instance.new("ParticleEmitter")

spark.Parent = firePart

end

stopFire()


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.