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

Reusing Code with script.Parent

Reusing Code with script.Parent

Prerequisites Students should have completed:
Lesson Time Approx 5 - 10 minutes
Optional Handouts Coding with Lua
Learning Objectives
  • Understand that objects can be organized in Parent/Child relationships
  • Use script.Parent to create a script that can be reused between parts

The code you’ve used so far won’t run if there are multiple parts of the same name. That means if you want multiple parts to change colors, you’d have to make new parts and new scripts for each of them. That could take forever.

Instead of doing that, you can make a small change to the code that will let you copy and paste it into any part.

Create a new Script and Part

Instead of creating a script in ServerScriptService, you’ll create a new script that’s attached to a part.

  1. Create a new part and rename it. This lesson will use ColorPart.
  2. Right-click the part, not ServerScriptService, and select Insert Object > New Script.
  3. Rename the script. This lesson will use ColorChangeScript.
  4. In the script editor, delete print("Hello world!").
  5. Copy and paste the code below into ColorChangeScript. This is the same script created in Loops and BrickColor.
-- Changes the color of loopingPart every few seconds

-- Create a variable to store the part
local loopingPart = game.Workspace.LoopingPart

-- Looping Code
while true do
    -- Changes loopingPart's color
    loopingPart.BrickColor = BrickColor.new(0.7, 0.9, 0.9)
    -- Wait 3 seconds before next instruction
    wait(3)
    loopingPart.BrickColor = BrickColor.new(0.9, 0.4, 0.9)
    wait(3)
    loopingPart.BrickColor = BrickColor.new(0.4, 0.5, 0.4)
    wait(3)
end

Modifying the Script to be Reusable

With the current script, you can only change the color of a single part named LoopingPart. To make the script usable with any part:

  1. Replace local loopingPart = game.Workspace.LoopingPart with below:
local loopingPart = script.Parent

  1. Change the comment since it’s no longer just changing LoopingPart.
-- Changes the color of the part this script is attached to
 
-- Create a variable to store the part
local loopingPart = script.Parent
 
-- Looping Code
while true do
    -- Changes loopingPart's color
    loopingPart.BrickColor = BrickColor.new(0.7, 0.9, 0.9)
    -- Wait 3 seconds before next instruction
    wait(3)
    loopingPart.BrickColor = BrickColor.new(0.9, 0.4, 0.9)
    wait(3)
    loopingPart.BrickColor = BrickColor.new(0.4, 0.5, 0.4)
    wait(3)
end

Parents and Children

A parent is anything that has objects, like scripts or parts, attached below it. Anything under the parent is its children. In the example shown below, ColorPart is the parent and ColorChangeScript is the child.

With the script and part just created, the part is the parent and the script is the child. This special line of code will tell the script to find it’s parent, the part. Using this line of code, you don’t even have to know what the part is named.

Duplicate and Test Parts

Now that the variable will refer to the whatever part the script is attached to, you can make as many copies as you want.

  1. Select the ColorPart in your game.
  2. Right-click the part and select copy, and then right-click again and select paste. You should end up with an exact duplicate of both the part and the script attached to it.
  3. Run the game to see the looping parts in action!

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.


Previous Loops and BrickColor