Loops
✏ Beginners Scripting Guide ✏
https://create.roblox.com/docs/tutorials/fundamentals/coding-4/repeating-code-with-while-loops https://create.roblox.com/docs/tutorials/fundamentals/coding-4/intro-to-for-loops https://create.roblox.com/docs/luau/control-structures#repeat-loops In this section, we will be discussing about loops. What are loops? Loops are code blocks that can store a block of code and repeat that block of code. In Roblox, we got for loops, while loop, and repeat loop. for loops can be used to countdown and loop through a table, a while loop can be used to repeat a block of code with a condition, and the repeat loop can be used to repeat a block of code until a condition is met. In a loop, we got "continue" and "break". Break is used to stop a current running loop and continue the code blocks that are below the loop and continue is used to move to the next iteration.
WHILE LOOP USAGE
local myBool = true
while myBool then -- If myBool is true then the loop is running
-- We insert a task.wait() to wait 1 second here so it doesn't crash the script and studio.
task.wait(1) -- Wait for 1 second before run the loop again.
print('The loop is running')
endFOR LOOPS USAGE
for i = 10, 0, -1 do
task.wait(1) -- Wait for 1 second before run the loop again.
print(i, ' seconds has passed') -- Will print "x seconds has passed"
endLoop through a table
local myTable = {
myString = 'Hello';
myBool = false;
myNumber = 1;
}
for i, v in pairs(myTable) do -- Pairs return both key and value!
print(i, v) -- This will print "myString & Hello, myBool & false, and myNumber & 1"
endOr:
REPEAT LOOP USAGE
BREAK USAGE
CONTINUE USAGE
Last updated