Many things in computer science or game development often repeat. For instance, an app might update content in a feed every few seconds, or a game might reward players a prize every day they're logged in. To accomplish this behavior, coders often use loops.
In computer science, a loop is a coding pattern that repeats a set of instructions, often until a specific condition is met. To practice this, you'll code a part that changes colors indefinitely. Later lessons will show how to stop the looping.
Setting Up the Part and Script
Use any Roblox project for this exercise. When finished, this part can be used as a way of decorating an environment.
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 World and write a comment stating the purpose of this script.
-- Changes the color of LoopingPart every few seconds
Using Variables to Store a Part
Variables can also hold references to objects besides strings. For example, when a script knows the reference to a part, the script can change it.
Rather than typing 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.
Under the comment, create a local variable by typing local loopingPart.
-- Changes the color of LoopingPart every few secondslocal loopingPartSet the variable to the looping part by typing = workspace.LoopingPart on the same line. Remember that the = operator sets the value of a variable to whatever is on the right of it.
-- Changes the color of LoopingPart every few secondslocal loopingPart = workspace.LoopingPart
While Loops
Before creating the while loop for the color changing part, it's worthwhile to explain its syntax. A while loop has three components:
- The while keyword.
- A condition, always after the keyword.
- Instructions, between the do and end keywords.
In loops, conditions can be a variety of logical statements. Some examples might be if two numbers are the same. A loop will run if its condition is true.
Notice in the sample below, the condition is true, meaning this loop will run forever. In later lessons, you'll code specific conditions that stop.
while true doprint("This loop is running")end
Coding a While Loop
Next, you'll set up the while loop for the color changing part.
On the next line type while true do. Then, press Enter to autocomplete and add the word end.
-- Changes the color of loopingPart every few secondslocal loopingPart = workspace.LoopingPart-- Looping Codewhile true doend
Adding Code in the Loop
With the while loop created, instructions can be added to change the part's color over time. Inside the loop, add a line of code for each color. Each color will use RGB values, a way of storing colors as numbers in computer science.
Between while true do and end, set the Color property of the part to Color3.fromRGB().
local loopingPart = workspace.LoopingPartwhile true doloopingPart.Color = Color3.fromRGB()endSetting the color can be done using a color picker in Studio. To do so, left click inside the () next to fromRGB. Then, click on the color wheel icon. Once you have a desired color, press OK to automatically add the color value in the code.
local loopingPart = workspace.LoopingPartwhile true doloopingPart.Color = Color3.fromRGB(82, 227, 255)end
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 task.wait() function.
On the line after changing the brick color, type task.wait(3). Whatever number placed inside the () is how many seconds the script will wait.
local loopingPart = workspace.LoopingPartwhile true doloopingPart.Color = Color3.fromRGB(82, 227, 255)task.wait(3)endUnder the wait function, repeat the same process of adding a new color. Finish it with an additional wait function.
while true doloopingPart.Color = Color3.fromRGB(82, 227, 255)task.wait(3)loopingPart.Color = Color3.fromRGB(177, 52, 255)task.wait(3)endTest the code and see if the colors change.
Troubleshooting Notes
At this point, if the color changing part doesn't work as intended, try one of the following below.
Issue: Colors are skipped
- Make sure you have a wait function between each color change, especially at the last line.
- Check that all the color change and wait code is between the while true do and end sections.
Issue: Part is still gray or doesn't change color as intended
- Make sure all RGB values have numbers from 0 to 255, are whole or decimals, and are separated by commas.
- The numbers inside the () of a wait function should be greater than 1. It's possible that if a wait is too short, you may not see that color.
Complete Color Looping Part Script
-- Create a variable to store the partlocal loopingPart = workspace.LoopingPart-- Looping Codewhile true do-- Changes loopingPart's colorloopingPart.Color = Color3.fromRGB(82, 227, 255)-- Wait 3 seconds before next instructiontask.wait(3)loopingPart.Color = Color3.fromRGB(177, 52, 255)task.wait(3)end
Summary
Loops are a common element in most computer languages. They are used to repeat instructions, sometimes until specific conditions are met. In this article, the while loop is used to repeat instructions forever. To create a while loop that repeats forever, use the syntax below, being sure to include instructions between the do and end keywords.
while true do-- Instructionsend