Let me first make it more clear.
It doesn't really have to be
for i, v in pairs(
end
Actually when it came out, developers quickly forgot what's the index and what's the variable inside of that. And later it was hard for them each time used it.
One day, a developer came with an idea on how to remember it.
Use I and V, what does they mean?
I = Index,
V = Variable
So that idea was absolutely great.
So that's why that's mostly called
for i, v in pairs
Now, what it's used for?
It's used to run to get a table and then run any function with that table.
For example, if you want to make a backwards countdown, you'll do this :
local myCountdown = {20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
for i, v in pairs(myCountdown) do
game.StarterGui.ScreenGui.TextLabel.Text = v
wait(1)
end)
Now. You see that "wait()" over there?
It's delaying the function in 1 secound,it can be placed over the function or under it. It won't matter so much, it will cause the same thing.
Now, I know it may seem to be not helpful. But when you'll really develop an advanced game you'll understand why it was so HELPFUL. I hope it was clear.
2 comments
Ahh, I see.. Thank you! c;
Take note that this is a generic for loop. There is also another for loop called a "numeric for loop" (for i = s,e,inc do) which is faster than a generic for loop. You can find examples of a numeric for loop and the link to the generic for loop wiki page here: http://wiki.roblox.com/index.php?title=Loops#For