Repeating Tasks with For Loops
Repeating Tasks with For Loops
There are different ways to make code run over and over. If you want the code to only run a certain amount of times, use a for loop. A good example, would be when creating countdown timers where the clock ticks down one second at a time. If you’re unsure of how long the code should repeat, or if you want it to repeat forever, that can be done with while loops as covered in Loops and BrickColor.
How For Loops Work
For loops use three values to control how many times they run: a control variable, an end value, and an increment value. Starting from the value of the control variable, the for loops will either count up or down each time it runs code inside the loop until it passes the end value. Positive increment values count up, negative increment values count down.
Steps in a For Loop
The loop then checks the control variable and starts over.
For example, if a loop has an end value of 10, once the control variable has passed 10, the for loop will stop.
Code a Practice For Loop
To see how a for loop works, use these steps to code a for loop that starts at 10 and counts down to 0, one number at a time. Every time the loop runs, it’ll print the current value inside the control variable.
- In ServerScriptService, create a new script named PracticeLoop.
- To begin a for loop, type the keyword
Keywords are words with specific purposes in code that can’t be used for anything but that purpose. For example, typing
while will start creating a while loop. Keywords cannot be used as variable names.
- Create a control variable named
countand set a starting value of 10.
Naming Control Variables
A control variable can be any acceptable variable name. Like other variable names, a control variable name should be clear and descriptive to what the for loop is doing.
- Set the end value to 0, by typing
, 0. Make sure to include a comma to separate the values.
- Create an increment value of -1 by typing
, -1. After the loop has finished its action, it’ll add the increment value to the control variable, count. Because the increment is negative, it’ll be subtracted when added to the control variable.
Increments can be Optional
A for loop doesn’t need an increment. Without a given increment, the for loop will by default add 1 after each loop. Because there isn’t a third number, you only need a comma to separate the control variable and end value, like in this loop:
for count = 10, 0
- To finish the for loop, type
doand press Enter to add
end. Any code typed between
endwill run each time the loop repeats.
- Now, to create a second-by-second countdown, just print the value of
countand use the wait function to create a timer.
for count = 10, 0, -1 do -- Prints the current number the for loop is on print(count) -- Wait 1 second wait(1) end
Because you’re printing a variable and not a string, you don’t need to include “quotation marks”.
- Run the project and watch the Output Window to see the for loop.
Troubleshooting Tips »
- Check that you have two commas separating the numbers in your for loop. Having extra or missing commas will make the loop not start.
- If the for loop prints all at once, make sure that there is a wait function that uses at least 1 second.
Different For Loop Examples
Changing the values of the control variable, end, and increment will change how the loop functions. The for loop you just wrote could instead count up to 10, or count up in odd numbers. Below are different examples of for loops with different start, end, and increment values.
Counting Up By One
Counting Up in Even Numbers
If For Loops Don't Run At All
If the control variable starts out beyond the end value, like in the example below, the for loop won’t run at all. In this case, the for loop is counting up and checking if count is greater than 0. When the for loop does it’s first check, it sees that 10 is greater than 0, and so it’ll stop the loop without printing anything.
Previous Powerups with if/then Statements Next Glowing Lights with For Loops