Loops

It is often needed to run some piece of code a set amount of times, for example if you'd like to access all items in a table, or rename all parts in the workspace.

Let's say that you need to print out numbers from 1 to 10. You could do it like this:

print(1)
print(2)
print(3)
print(4)
-- 2 hours later...
print(10)

This will work, but it is inefficient. Or you could use loops and do it this way:

for i = 1, 10 {
    print(i)
}

This will result in the same output as the previous example, but it's much more efficient and readable. So let's explore loops and see how they can be helpful for us.


There are three types of loops: for, while and tableloop. Each serves a different purpose.

for loops

The for loop iterates a number from a beginning value to an end value. Let's use the previous example:

for i = 1, 10 {
    print(i)
}

This will initialize the variable i, set it to 1, and execute the attached code (print(i)) and increase i by 1 each time, until it reaches 10.

The output of this will be: 1 2 3 4 5 6 7 8 9 10

You can specify how much the number needs to be increased by, using a third step parameter. Let's adapt our previous example so i is increased by 2 instead of 1:

for i = 1, 10, 2 {
    print(i)
}

Now the output will be: 1 3 5 7 9

The step can be any number, even negative or with a decimal point (or both). However with a negative step, the maximum must be smaller than the minimum.

while loops

The while loop will execute repeatedly while a certain condition is true. For example, let's say we have some kind of part workspace.Part, and we want to execute some code until it becomes green. We would do it this way.

while workspace.Part.BrickColor ~= BrickColor.new("Lime green") {
    wait(1)
    print("Not green")
}
print("Green")

The ~= operator is the "not equal" sign, it will be explained in the next article.

Note the use of wait(1) to wait one second before the loop is executed again. If a single loop completes very quickly, but the directive runs for extended periods of time, the script will freeze until the directive is over. By placing wait(1), we force each loop to take at least one second to execute, therefore avoiding freezing.

It is also possible to run a while loop forever:

while true {
    wait(1)
    print("Still running!")
}

This will continue to run forever, until the game ends or the script is manually stopped.

tableloop loops

The tableloop loop will loop through all items or keys in a table. For example, say we have a myTable consisting of elements 1, 2, "bananas", 4. We can loop through all 4 elements like this:

myTable = table.create(1, 2, "bananas", 4)

tableloop key, value in myTable {
    print(value)
}

The output will be 1 2 bananas 4. Note the use of key and value variables: the key variable holds the current item's index, while value holds the current item itself. Also observe that the table's name and the variables are separated using in. tableloop works with both arrays (lists) and dictionaries.

In Lua, you would loop through a table like this: for key, value in pairs(myTable) do. This is not valid syntax in Powerlang, as the for loop does not support looping through a table. This is why tableloop is used instead. It is exclusive to Powerlang and mirrors the Loop Through Table block in RetroStudio Visual Scripting.

Controlling loops

It is possible to control loops inside of loops themselves using the break and continue statements.

The break statement will cause all loops to stop entirely, proceeding with the code after it. For example:

for i = 1, 10 {
    if i == 3 {
        break
    }
    print(i)
}
print("Done!")

The break statement will activate when i reaches 3, therefore the loop will stop completely at the number 3. The output would be as follows: 1 2 Done!

The continue statement will cause a loop to skip the current loop. Using the same example, but replacing break with continue:

for i = 1, 10 {
    if i == 3 {
        continue
    }
    print(i)
}
print("Done!")

This time the value 3 will be skipped, and the output will be: 1 2 4 5 6 7 8 9 10 Done!


This is all for loops. On the next page we will learn about conditions.

Last updated