I've been trying to understand the 3 types of loops in scripting but when i look on the wiki it doesn't give a clear example of one working every time i try to make prints I've made it errors on because I don't understand how to do it. if you would please show me how with codes that loop prints or anything on all 3 types of loops it would help me understand how the loops work and its "rules", or tell me the rules and how its supposed to be done.Thanks
and
while (condition) do wait(); -- something end
If you want it to run forever, do while true do
or while (true) do
(no difference).
But the reason I say condition is because you could make it require a variable, say local run = true
to remain true.
local run = true; while run do -- ... end
Ideally if you need an infinite loop you use a while loop. Just make sure to include a wait()
For loops are useful for iterating (counting). They have many uses, I'll try to touch on each of them:
for i = 1, NumberToCountTo, 1 do end
The first item, i = 1
is a variable, it's where you start counting from.
The second item NumberToCountTo
is the number you want to count to.
The third item, 1
(in this case) is the number to count by.
Example, this below will print the numbers between 1 and 50.
for i = 1, 50, 1 do print(i); wait(); end
Now let's say you want to print the name of all the parts in workspace:
for i, child in pairs(workspace:GetChildren()) do print(child.Name); wait(); end
In this case, child
is a second variable.
You could also print an entire array (table) like so:
local Table = {"apple", "orange", "potato", "bean", "dog", "cat"}; for i = 1, #Table, 1 do print(Table[i]); end
#Table
returns the length of the table we want to iterate (count) through.
Now finally,
Repeat loops work like this
repeat -- some task wait(); until -- Until some event happens to make it stop
Here is an example to print hi until you have printed hi 10 times: (though for loops are better for this)
local i = 1; repeat print("hi"); i = i + 1; until i == 10;
I hope this helps!
While true do loop-
while true do print("hi") wait(1) end
whatever is contained in this loop keeps on going until true(which u can make a variable)=false. But make sure to to put a wait in there or ur game is going to crash. In this case the output would say: hi. It would wait one second and say hi again, and again, and it keeps on going.
For loop-
for i=1, 10 print(i) print("hi") end
i= a counter I guess u can say. Put a starting and ending point. ex. 1,10. This would say in the output: 1 2 3 4 5 6 7 8 9 10. There is a hidden value which goes after the ten in this case and it is the number it counts by. ex. 1,10,2. This would count by twos. This for loop would print hi as long as the counter hasn't hit 10.
Repeat loop-
local i = 1 repeat print("hi") i = i + 1 until i == 10
Basically, repeat repeats everything from repeat to until. It keeps on going until until=true. If until=true then then the rest of the repeat code before it is "shut down" and doesn't work, and the code after until executes. In this case, hi would be printing until i = 10 and it would stop.
This is my understanding of how these 3 basic loops work. Please accept my answer if this helps:).
~rilgundam