Hey everyone! Welcome back to my beginner Roblox scripting tutorial series. In today’s video, we’re going to talk about loops, which are super important when you’re coding — not just in Roblox, but in any programming language.
So let’s jump right into it.
Why Loops Are So Useful
Let’s say you want to print the same thing multiple times. You might write something like this:
print("Statement 1")
print("Statement 2")
print("Statement 3")
Okay, that’s simple enough. But what if you wanted to print these same three lines — not just once — but five times?
Sure, you could just copy and paste that code block five times. That would look like this:
print("Statement 1")
print("Statement 2")
print("Statement 3")
print("Statement 1")
print("Statement 2")
print("Statement 3")
-- and so on...
But that’s not really efficient. It’s long, repetitive, and messy. And if you wanted to change something later, you’d have to update every single block. That’s where loops come in.
How the For Loop Works
So instead of repeating all that code, you can just use a for loop to do it for you. Here’s what that looks like:
for myCounter = 1, 5, 1 do
print("Statement 1")
print("Statement 2")
print("Statement 3")
end
What’s happening here is that the loop starts with a counter — in this case, myCounter
— that begins at 1 and runs until it reaches 5. The number at the end — that 1
— is called the step, which just means how much the counter increases each time.
So, on the first run, myCounter
is 1. Then it becomes 2, then 3, and so on, until it hits 5. Once it passes 5, the loop stops. That means those three print statements get executed five times, which is exactly what we wanted — but with much less code.
Changing the Step Value
Now, what if we don’t want to count by one? Let’s say we want to go by twos.
We can just change the step value like this:
for myCounter = 2, 6, 2 do
print("Hello world")
end
Now, myCounter
starts at 2 and goes to 6, increasing by 2 each time. So it will print “Hello world” three times — when the counter is 2, 4, and 6.
Reversing a Loop
You can also loop backwards by using a negative step. Let’s look at this example:
for i = 5, 1, -1 do
print("Counting down...")
end
In this case, the loop starts at 5 and goes down to 1, subtracting 1 each time. So it’ll run when i
is 5, 4, 3, 2, and 1. Once i
goes below 1, the loop stops.
Introducing the While Loop
Okay, now let’s talk about another kind of loop called the while loop.
With a for loop, you usually know ahead of time how many times it will run. But a while loop is different — it keeps going as long as a condition is true. That makes it useful when you’re not sure exactly how many times something needs to repeat.
Here’s an example of a while loop:
local myCounter = 1
while myCounter <= 5 do
print("Statement 1")
print("Statement 2")
print("Statement 3")
myCounter = myCounter + 1
end
So, what’s happening here? First, we create a variable called myCounter
and set it to 1. Then the while
loop checks: is myCounter
less than or equal to 5? If yes, it runs the three print statements, then it increases myCounter
by 1.
Once myCounter
becomes 6, the condition is no longer true, so the loop stops.
Watch Out for Infinite Loops!
Be careful, though — if you forget to increase the counter inside your while loop, you can create an infinite loop, which means your code will never stop running. That can freeze or crash your game.
Here’s what a bad while loop looks like:
local myCounter = 1
while myCounter <= 5 do
print("Statement 1")
print("Statement 2")
print("Statement 3")
-- Oops! We forgot: myCounter = myCounter + 1
end
Since myCounter
never changes, the condition myCounter <= 5
will always be true, and the loop will go on forever. Always make sure your condition will eventually become false!
Nesting Loops
One last thing: you can even put one loop inside another loop. That’s called a nested loop, and it’s super powerful.
For example:
for i = 1, 3 do
print("A")
for j = 1, 5 do
print("B")
end
end
Here, for each time i
runs from 1 to 3, we print “A”, and then the inner loop runs, printing “B” five times. So the output looks like this:
A
B
B
B
B
B
A
B
B
B
B
B
A
B
B
B
B
B
This is useful for things like spawning enemies in waves or working with grids of objects.
Wrapping Up
So to recap — for loops are great when you know exactly how many times something should repeat. While loops are better when you want something to keep going until a certain condition changes. And if you ever need more complexity, nested loops can help you get the job done.
That’s it for this tutorial! Hopefully now you’ve got a solid understanding of how loops work in Roblox Lua. Play around with them, try changing the values, and watch what happens. The more you practice, the easier this will get.
See you in the next Blog!