Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.
Repeat code with while loops
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.
Set 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 Worldand write a comment stating the purpose of this script.-- Changes the color of LoopingPart every few seconds
Use 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.
Note
A local variable only works in the script where it was created. If a local variable named part1 is created in Script1, it cannot be used in Script2. Most variables you use will be local variables.
Under the comment, create a local variable by typing
local loopingPart.-- Changes the color of LoopingPart every few seconds local loopingPartSet the variable to the looping part by typing
= workspace.LoopingParton 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 seconds local loopingPart = workspace.LoopingPart
Note
Remember that the = operator sets the value of a variable to whatever is on the right of it.
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
whilekeyword. - A condition, always after the keyword.
- Instructions, between the
doandendkeywords.
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 do
print("This loop is running")
end Code 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 wordend.-- Changes the color of loopingPart every few seconds local loopingPart = workspace.LoopingPart -- Looping Code while true do end
Note
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. Instructions in a loop should always be indented.
Add 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 doandend, set the Color property of the part toColor3.fromRGB().local loopingPart = workspace.LoopingPart while true do loopingPart.Color = Color3.fromRGB() endSetting the color can be done using a color picker in Studio. To do so, left click inside the
()next tofromRGB. 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.LoopingPart while true do loopingPart.Color = Color3.fromRGB(82, 227, 255) end
Note
If desired, a color can be created manually by assigning a value in each number from 0 to 255.
Make 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.LoopingPart while true do loopingPart.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 do loopingPart.Color = Color3.fromRGB(82, 227, 255) task.wait(3) loopingPart.Color = Color3.fromRGB(177, 52, 255) task.wait(3) end
Note
As you add more code in the loop, make sure it's all in scope, meaning code is in the right place. In order for code to run in the loop, it has to be between the while true do and before end.
Test 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 doandendsections.
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 part
local loopingPart = workspace.LoopingPart
-- Looping Code
while true do
-- Changes loopingPart's color
loopingPart.Color = Color3.fromRGB(82, 227, 255)
-- Wait 3 seconds before next instruction
task.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
-- Instructions
end