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

  1. Create a new part with a descriptive name. This lesson uses LoopingPart.
  2. In ServerScriptService, add a new script named LoopingScript.
  3. In the script, delete Hello World and write a comment stating the purpose of this script.

Using Variables

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 local nameOfVariable

A local variable only works in the script where it was created. Most variables you use will be local variables.

  1. Under the comment, create a local variable named loopingPart by typing local loopingPart.
  2. Set the variable to the looping part by typing = game.Workspace.loopingPart on 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)

  • Examples: loopingPart, playerHealth

Create a while true do Loop

Next, you’ll create the while loop which will hold all of the code that should repeat.

  1. On the next line type while true do.
  2. Press Enter. The word end should autocomplete for you.
Indenting Code

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

Everything between while true do and end will loop forever. Inside the loop, add a line of code for each color.

  1. Between while true do and 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 wait() function.

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 print() function.

Add a wait() Function

Add a wait function to make the code wait 3 seconds.

  1. On the line after changing the brick color, type wait(3).

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.

  1. Under the wait function, set loopingPart.BrickColor to a new color.
  1. Add a second wait function. If you don’t, the second color will blink so fast you won’t see it.
  1. Test 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 do and end.

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.

  1. 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 with script.Parent