Loops and BrickColor

Loops and BrickColor

  • Create a Script
  • Properties and BrickColor
  • Lesson Time 10 - 15 minutes
    Optional Handouts Coding with Lua
    Learning Objectives
  • Create custom local variables to store a part
  • Use the = sign to change a variable
  • Use the Wait function and While True Do to create parts that loop through colors
  • 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

    1. Create a new part with a descriptive name like LoopingPart, or RainbowPart. This lesson will use LoopingPart.
    2. Create a new script under ServerScriptService.
    3. Name the script LoopingScript.
    4. Delete Hello World.
    5. Write a descriptive comment.

    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.

    Creating Variables

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

    1. Under your first comment create a local variable named loopingPart by typing local loopingPart.
    2. 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)
    Examples: LoopingPart, EndZone
    Variables: camelCase (first letter lowercase)
    Examples: loopingPart, playerHealth

    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

    1. Write a new comment describing what you are about to code. For example: Looping code.
    2. On the next line type while true do.
    3. Press Enter on the keyboard. 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, type code to change the part’s BrickColor property to a new set of RGB values.

    1. Between while true do and end type:
    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 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 the BrickColor changing code, type wait(3).

    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

    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.

    1. Under the wait function, set loopingPart.BrickColor to a new color.
    2. Add a second wait function. If you don’t, the second color will blink so fast you won’t see it.
    3. Test your code.
    4. Continue adding colors and wait functions 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 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.

    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.