I am trying to loop some things inside of my game but I just got started on learning loops? Does anyone have any useful information?
How does a loop work? I just got started and want to know?
3 answers
Well, there are multiple types of loops, lemme walk you through them.
You have a while wait() loop, which looks like this.
while wait(2) do --Does something every 2 seconds
end
You have while true do.
while true do --//Constantly Runs
wait() --//Always put a wait so it won't crash studio
end
You have repeats.
repeat --//Runs until something or someone does something
wait() --//Always put a wait
until --//When you want the repeat to end, just put something until db == true
And last, but not least, for i, v in pairs loop
for i, v in pairs(workspace:GetChildren()) do --//In this example, this loop gathers everything in the workspace
--//Whatever you need in here
end
And that's pretty much it, hoped this helped ya!
3 comments
There is also i, v in pairs loop so i think you should include that in your code so he could see.
Oh yeah lol, forgot about that one.
@goodlead it's a type of for loop so it shouldn't count as a main loop.
There always a loop in any generic programming language (Python, C#, Lua, etc.) A loop is basically code that is repeated multiple times. There are 3 (main) types of loops in Roblox Lua. While Loop, For Loops, and Repeat-Until Loops. While Loop: A while loop repeats until a certain condition is met. So for example, this code:
local n = 10
while n ~= 0 do
wait(1) --We always put a wait in while loop so that studio doesn't crash
n = n - 1
end
It will keep looping until n = 0. To loop programs forever, you can use while true do , however, it will break your script if you do not put a wait in it. So to loop things forever, I recommend you do this:
while wait() do --wait() has a default wait time that is very short.
print("While loop is printing")
do
Next is for loop. A for loop is similar except it repeats how many times you want it to.
for i, 10, 1 do --i is the starter number, always starts at 0, 10 is the maximum number, and 1 is how many times you want it to count.
print(i)
end
Next is repeat-until loops. Repeat-Until is very similar to a while loop (and probably the most complex one) except it runs the code once, so for example when n = 10, it will run the code, and then stop.
local n = 0
repeat
n = n + 1
until n = 10
There is more information here: Loops
NumberDoge It is Badimo3456p from the chat, the loop makes it repeat again.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Loops is a type of code that will repeat the code inside it