Reusing Code with script.Parent
Reusing Code with script.Parent
Students will be able to:
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.
- Create a new part and rename it. This lesson will use ColorPart.
- Right-click the part, not ServerScriptService, and select Insert Object > New Script.
- Rename the script. This lesson will use ColorChangeScript.
- In the script editor, delete
- 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:
local loopingPart = game.Workspace.LoopingPartwith below:
local loopingPart = script.Parent
- 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.
- Select the ColorPart in your game.
- 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.
- 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.