When you know how many times to run, let for keep the counter — reward every player, spawn 5 obstacles, print 1 to 10.
When you know in advance exactly how many times to run, for is the cleanest loop in the language — it keeps its own counter, so you don't have to.
for i = 1, 5 do
print("Round " .. i)
end
This runs the block 5 times, with i as 1, then 2, then 3, 4, 5. The for loop keeps its own counter — no manual bookkeeping like the while version needed.
This is how you give every player in the server a reward, give out 10 items, or spawn 5 obstacles.
for i = 1, 3 do print('Hi') end — how many times does it print 'Hi'?
The counter isn't just decoration — it's a variable you use inside the loop:
for i = 1, 5 do
print("Checkpoint " .. i)
end
Here i makes each message unique. You'll use this constantly: numbering spawns, labeling rounds, building a row of obstacles where each one's position depends on i.
for i = 1, 4 do print(i) end — what does the last printed number equal?
A third number controls the step:
for i = 5, 1, -1 do -- 5, 4, 3, 2, 1
print("Launching in " .. i .. "...")
task.wait(1)
end
for i = 1, 10, 2 do -- 1, 3, 5, 7, 9
print(i)
end
The first example is a countdown — a negative step counts down. The second skips by 2. The step is optional and defaults to 1.
for is the right tool when you know the stop point in advance. If you're waiting for a condition to change — coins reaching 100, say — while or repeat is the tool instead.
for needs no manual counter. Don't write i = i + 1 inside a for loop — it already counts.for for changing conditions. If the end depends on game state, use while.Use a for loop to print 'Spawn' five times.
-- your for loop
Next: putting lists of things in one box — tables.