Understanding and Using Simple Loops

Loops help you run code multiple times without repeating it constantly. This way, you can easily accomplish tasks like displaying numbers or iterating through lists.

Here's how:

  1. What is a loop: A loop repeats a block of code as many times as you want.
  2. for loop: With the for loop, you can run code, for example, from 1 to 5.
  3. while loop: This loop runs as long as a condition is true, for example until a number becomes greater than 5.
  4. Using loops: You can use loops to automatically perform multiple tasks, such as changing multiple parts.
  5. Avoid infinite loops: Make sure the loop stops eventually, otherwise your game will freeze.

With loops, you can write your code shorter and more efficiently!

-- Dieses Script kommt in ServerScriptService
for i = 1, 5 do
    print("Das ist Durchlauf Nummer: " & i) -- Counts from 1 to 5
end

local count = 1
while count <= 5 do
    print("while-Schleife Nummer: " & count) -- Repeats until 5
    count = count + 1
end