Can someone just explain to me in as much detail as possible what a for loop is and how it works and what stuff can it do?
✓ ANSWER ACCEPTED
What is a for loop and what can it do?
2 answers
✓ Accepted answer
There are two types of loops, a numeric for loop, and a generic for loop. They are both mainly used for iterating through tables of different kinds. Although the numeric for loop is also commonly used for countdowns.
Generic For
local tab={"Hello"}
for index, value in pairs(tab) do
print(index, value)
end
Will print "1 Hello."
Numeric For
local tab={"Hello"}
for index=1, #tab do
print(index, tab[index])
end
Will print "1 Hello" as well.
Countdown
local t=30 --time to countdown from
for index=t, 1, -1 do
print(index)
wait(1)
end
For loops can be used for many things, like irritating through tables or making a loop go 10 times.
--This loop goes through a table and prints its index and value.
myTable = {"Thetacah", "Josh", "John"};
for i, v in pairs(myTable) do
wait(1)
print(i..v)
end
--This loop will print one through ten since it runs 10 times and i is the value of the ammount of times the loop runs.
for i = 1, 10 do
wait(1)
print(i)
end
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
for the countdown one couldnt you just do for t,1,-1 do print(t) ??