Loops: for, while & repeat
Understanding Loops in Luau
Loops are essential for executing a block of code multiple times. In Luau, there are several types of loops: numeric for loops, generic for loops, while loops, and repeat-until loops. Each type serves different purposes and can be combined with control statements like break and continue. Let's explore each of these in detail.
Numeric For Loops
Numeric for loops are used when you know in advance how many times you want to iterate through a block of code. Here’s the basic syntax:
for i = start, finish, step do
-- Code to execute
endThe start is the initial value, finish is the final value, and step is the increment (which defaults to 1 if not specified).
Here’s an example that prints numbers from 1 to 5:
for i = 1, 5 do
print(i)
endGeneric For Loops
Generic for loops are used to iterate over collections such as tables. You can use ipairs for ordered tables and pairs for unordered tables. The syntax is:
for key, value in pairs(table) do
-- Code to execute
endHere’s an example using ipairs:
local fruits = {"Apple", "Banana", "Cherry"}
for index, fruit in ipairs(fruits) do
print(index, fruit)
endAnd an example using pairs:
local playerScores = {Alice = 100, Bob = 150, Charlie = 200}
for player, score in pairs(playerScores) do
print(player, score)
endWhile Loops
While loops continue to execute as long as a specified condition is true. The syntax is:
while condition do
-- Code to execute
endHere’s an example that counts down from 5:
local count = 5
while count > 0 do
print(count)
count = count - 1
endRepeat-Until Loops
Repeat-until loops are similar to while loops, but they guarantee that the code inside the loop will run at least once. The syntax is:
repeat
-- Code to execute
until conditionHere’s an example that continues until a condition is met:
local number
repeat
print("Enter a number (0 to exit):")
number = tonumber(io.read())
until number == 0Using Break and Continue
The break statement can be used to exit a loop prematurely, while continue allows you to skip the current iteration and move to the next one. Here’s how to use them:
for i = 1, 10 do
if i == 5 then
break -- Exit the loop when i is 5
end
print(i)
endFor a continue effect, you can use if statements:
for i = 1, 10 do
if i % 2 == 0 then
continue -- Skip even numbers
end
print(i)
endPreventing Freezing with task.wait
When using loops, especially in game development, it's crucial to avoid freezing the game. You can use task.wait() to yield execution and allow other processes to run. Here’s an example:
for i = 1, 10 do
print(i)
task.wait(1) -- Wait for 1 second before the next iteration
endThis approach ensures your game remains responsive while executing the loop.
Conclusion
Loops are powerful tools in Luau that help you automate repetitive tasks efficiently. By understanding the different types of loops and how to use them effectively, you can create more dynamic and responsive Roblox games. Remember to use task.wait() within loops to maintain game performance.