Loops in Luau: For, While, and Repeat Made Easy

Learn how to use for, while, and repeat loops in Roblox Luau to automate repetitive tasks and create dynamic gameplay experiences.

Hey there, future Roblox developer! Today we’re diving into one of the most powerful tools in your scripting toolbox: loops. If you’ve ever thought “I don’t want to write the same code 100 times,” loops are your answer. They let you run the same code multiple times without copying and pasting, making your scripts cleaner and way more powerful.

Let’s explore the three types of loops in Luau: for, while, and repeat. By the end of this tutorial, you’ll know exactly when and how to use each one!

What Are Loops and Why Do We Need Them?

Imagine you want to create 50 parts in your game, or check every player’s score, or make a countdown timer. Without loops, you’d have to write the same code over and over again. Loops automate repetitive tasks by running a block of code multiple times based on conditions you set.

Think of loops like this: instead of telling someone “take one step forward” fifty times, you say “take one step forward, and repeat this 50 times.” Much simpler, right?

The For Loop: Perfect for Counting

The for loop is your go-to when you know exactly how many times you want something to repeat. It’s great for counting through numbers or going through lists of items.

Numeric For Loop

Here’s the basic structure:

for count = 1, 10 do
    print("Count is:", count)
end

This loop will print numbers 1 through 10. Let’s break down what’s happening:

  • count is a variable that changes each time the loop runs
  • 1 is where we start
  • 10 is where we stop
  • Everything between do and end runs each time

You can also count by different amounts using a third number called the increment:

for count = 0, 20, 5 do
    print("Count is:", count)
end
-- Prints: 0, 5, 10, 15, 20

Want to count backwards? Use a negative increment:

for count = 10, 1, -1 do
    print("Countdown:", count)
end
print("Blast off!")

Generic For Loop (For Pairs/IPairs)

When you need to go through a table or array, use the generic for loop with ipairs or pairs:

local fruits = {"Apple", "Banana", "Orange"}

for index, fruit in ipairs(fruits) do
    print(index, fruit)
end

This is incredibly useful for checking all players in a game, all parts in a folder, or any collection of items!

The While Loop: Keep Going Until Something Changes

A while loop keeps running as long as a condition is true. Unlike for loops, you don’t need to know how many times it’ll run—it just keeps going until the condition becomes false.

local health = 100

while health > 0 do
    print("Still alive! Health:", health)
    health = health - 10
    task.wait(1)
end

print("Game over!")

This loop checks the condition before each repetition. If health > 0 is false at the start, the code inside won’t run at all.

Important tip: Always make sure your while loop will eventually stop! If the condition never becomes false, you’ve created an infinite loop that can crash your game. That’s why we include task.wait() and change variables inside the loop.

When to Use While Loops

While loops are perfect for:

  • Waiting for a player to join
  • Running game logic until a match ends
  • Checking if something exists before proceeding
  • Creating timers that depend on game state

The Repeat Loop: Do First, Check Later

The repeat loop is similar to a while loop, but with one key difference: it always runs at least once because it checks the condition after running the code.

local number = 0

repeat
    print("Number is:", number)
    number = number + 1
until number >= 5

Notice that the condition comes at the end with until. The loop repeats until the condition becomes true (the opposite of while, which continues while the condition is true).

When to Use Repeat Loops

Repeat loops are less common, but they’re useful when:

  • You need the code to run at least once no matter what
  • You’re validating user input and want to keep asking until they get it right
  • You’re generating random values until you get one that meets your criteria

Common Mistakes to Avoid

As you practice with loops, watch out for these common pitfalls:

  • Infinite loops: Always ensure your loop has a way to end
  • Forgetting task.wait(): In while and repeat loops that might run many times, add task.wait() to prevent script timeout
  • Off-by-one errors: Remember that for i = 1, 10 includes both 1 AND 10 (that’s 10 iterations, not 9!)

Quick Recap

You now know all three loop types in Luau:

  • For loops are best when you know how many times to repeat or need to go through a list
  • While loops keep going as long as a condition is true (checking before each run)
  • Repeat loops run at least once, then keep going until a condition becomes true

Loops are essential for creating dynamic, interactive Roblox games. Practice by creating a countdown timer, spawning multiple parts, or cycling through player lists!

What’s next? Now that you understand loops, you’re ready to learn about functions—reusable chunks of code that make your scripts even more organized and powerful. Keep scripting, and have fun building!

Author

Leave a Reply

Your email address will not be published. Required fields are marked *