Loops and BrickColor
Loops and BrickColor
You don’t have to be limited to just one color when coding. Your parts can be a magical unicorn forest where colors change every second or loop through the colors of your favorite superhero!
Setup a Part and Script
- Create a new part with a descriptive name like LoopingPart, or RainbowPart. This lesson will use LoopingPart.
- Create a new script under ServerScriptService.
- Name the script LoopingScript.
- Delete Hello World.
- Write a descriptive comment.
Teaching Tip - Writing Comments »
Encourage students to write comments in their own words. Offer feedback to ensure comments are clear and specific.
- Needs improvement: Changes the brick.
- Specific: Changes color of Looping part every 5 seconds.
Not only can variables hold strings, they can also hold directions to an object. Typing
game.Workspace.NameOfYourPart over and over is pretty boring. To save time, that entire line can be stored inside a local variable.
Create a Local Variable
A local variable works in the script where it is coded, most variables you create will be local variables. They’re created by typing local and then the name of the variable, for example
- Under your first comment create a local variable named loopingPart by typing
- Set your variable to the location of the part’s BrickColor property by typing
local loopingPart = game.Workspace.loopingPart.
If your part is named LoopingPart, your script should now look like:
Using the Equal Sign =
In coding, 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)
Variables: camelCase (first letter lowercase)
Using Loops - The While True Do loop
The code you made before ran once and then stopped. To make your code repeat use a while true do loop. Loops repeat until something tells them to stop. We’re not going to tell the code to stop, it’ll run forever!
Create the while true do Loop
- Write a new comment describing what you are about to code. For example:
- On the next line type
while true do.
- Press Enter on the keyboard. 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, type code to change the part’s
BrickColor property to a new set of RGB values.
while true doand
loopingPart.BrickColor = BrickColor.new(0.7, 0.9, 0.9)
Don't Run the Script Yet
If you run the script now, you'll get an error that may say "A script has been running for a long time, and may be hanging." If you encounter this error, press "Kill Script". The script is trying to run the color change non-stop and needs something to slow it down.
Creating RGB Values
To create a new color remember to: Use 3 decimal numbers between 0 and 1. One each for red, green, and blue, each separated by commas.
Coding within 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.
Adding Time Between Color Changes
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 the BrickColor changing code, type
Number Type Variables
wait() takes a number variable type instead of a string variable. Number variable types hold only numbers and do not use quotation marks.
- Correct: 5, 6.5, 22
- Incorrect: five, "6.5", twenty2
Teaching Tip - Use Run instead of Play »
When testing small parts of the game, have students Run the game. This runs the entire game without creating a player, reducing distractions for students and making testing quicker. You can switch from Play to Run by clicking the arrow under Play:
Adding a Second Set of Colors
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.
- Continue adding colors and
waitfunctions until you have as many colors as you want.
Finished Code Example
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.
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.