Loops and BrickColor
Loops and BrickColor
Parts don’t have to be limited to just one color. They can loop through a list of your favorite colors to create a unicorn forest or a tribute to your favorite sports team.
Make code repeat using a
while true do loop. This type of loop runs until something tells it to stop. Since we’re not going to tell the code to stop, it’ll run forever!
Setup a Part and Script
- Create a new part with a descriptive name. This lesson uses LoopingPart.
- In ServerScriptService, add a new script named LoopingScript.
- In the script, delete
Hello Worldand write a comment stating the purpose of this script.
Not only can variables hold strings, they can also hold directions to an object.
Rather than typing
game.Workspace.NameOfYourPart over and over, that entire line can be stored inside a local variable. Local variables are created by typing local and then the name of the variable, for example
A local variable only works in the script where it was created. Most variables you use will be local variables.
- Under the comment, create a local variable named
- Set the variable to the looping part by typing
= game.Workspace.loopingParton the same line.
Using the Equal Sign =
In computer science, the
= sign doesn’t have the same meaning as it does in math. It doesn’t mean that things are equal. Instead, it changes the value of a variable.
Naming Rules - Variables and Parts
Roblox developers have rules when naming parts and variables to help them know what’s a variable and what’s a part.
Parts: PascalCase (first letter capitalized)
- Examples: LoopingPart, EndZone
Variables: camelCase (first letter lowercase)
Create a while true do Loop
Next, you’ll create the
while loop which will hold all of the code that should repeat.
- On the next line type
while true do.
- Press Enter. The word
endshould autocomplete for you.
You might have noticed the editor automatically added
end to the script and indented the next line of code. Indenting makes code easier to read.
Add Code Inside the Loop
while true do and
end will loop forever. Inside the loop, add a line of code for each color.
while true doand
end, change the brickColor of the looping part.
Don't Run the Script Yet
If you run the script now, you’ll get an error. This is because the script changes colors so frequently, causing Studio to overload. You’ll address this later by making the script wait after each color change.
Keep Code in Scope
In order for code to run in the loop, it has to be between the
while true do and before
end. Having your code in the right place so that it runs correctly is referred to as being in scope.
Making the Script Wait
If you add a second line of color changing code right now, it would change the brick’s color so fast you might not even see the first color go by. To make the script wait before running the next line of code, use a
Functions are pre-programmed chunks of code. Rather than having to type all that code out again, coders create functions as shortcuts. Coders really like shortcuts. Hello World used a
Add a wait() Function
Add a wait function to make the code wait 3 seconds.
- On the line after changing the brick color, type
Using Different Wait Times »
You can change the wait time as long as you include a number variable type between the parenthesis. Number variable types hold only numbers, and cannot include strings or quotations.
- Correct: 5, 6.5, 22
- Incorrect: five, “6.5”, twenty2
Adding a Second Color
You have one color, now you need a second color for your code to switch back and forth between.
- Under the wait function, set
loopingPart.BrickColorto a new color.
- Add a second wait function. If you don’t, the second color will blink so fast you won’t see it.
- Test your code.
Troubleshooting Your Code »
Issue: Colors are skipped
- Make sure you have a wait function between each color change, especially at the last line.
- Make sure any code that you want to loop is between
while true doand
Issue: Part is still gray or doesn’t change color as intended
- Make sure all RGB values have numbers from 0 to 1, are decimals, and are separated by commas.
- Continue adding colors and wait functions until you have as many colors as you want. Try different RGB values and times for wait functions.
Finished Code Sample
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 Properties and BrickColor Next Reusing Code w/ script.Parent