Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
7

Can someone explain this "for i,v in pairs" to me? [closed]

Asked by 9 years ago

I have seen tons of complicated things about this little part of a script.

for i,v in pairs """"" do
-- code
end

I have no idea how to use it. Please can someone explain or give me a link to let me understand this. If possible give an example? :)

Also I have a question about it. Can you change the i,v part in the script?

Thanks in advanced! :)

Locked by PrismaticFruits, JakyeRU, and User#5423

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
5
Answered by
Ekkoh 635 Moderation Voter
9 years ago

It's called an enhanced for loop. The i, v is defining variables for each index and value of the table that you are iterating through. k for key is often used rather than i in most cases. pairs is your iterating function, it returns each index and value of the table. Another not so common one is ipairs, which functions the same for the most part except that it will stop the loop once it runs into a non-existent (nil) value. For example:

local myTable = {5, "pineapple", [23] = 6, ["foo"] = true}
for key, value in pairs(myTable) do
    print(key, value)
end

Output:

1       5           -- because we didn't define a key for the value 5, it defaulted to index 1.
2       pineapple   -- same case here, except they index is now 2.
23      6           -- we defined a key by using square brackets and we set the index to 23.
foo     true        -- keys can also be strings, and we can store any value we want in a table.
0
Ahh, I see.. Thank you! c; WelpNathan 307 — 9y
0
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 Spongocardo 1991 — 9y
Ad
Log in to vote
0
Answered by
RedCombee 585 Moderation Voter
9 years ago

Here's an example.

for i,v in pairs(game.Workspace:GetChildren()) do
    --code
end

In this example, the argument inside the parentheses is a table. The game forms a table out of the Workspace's children. i = Index, v= Value (I believe this is right). What that means is it's like the other kind of for loop, but it's a little different. The index is essentially what item the loop is going through in the table, and the v is the item itself.

And yes, i and v are changeable. You can make them any other variable name you can come up with, such as:

for scripting,helpers in pairs() do
--code
end
0
GetChildren returns a numerically indexed table, so i would represent what position that specific object holds in the table (i.e. the first object will be i=1) Ekkoh 635 — 9y
Log in to vote
0
Answered by
XDvvvDX 186
4 years ago
Edited 4 years ago

Let me first make it more clear. It doesn't really have to be

~~~~~~~~~~~~~~~~~ for i, v in pairs(--Table) do --Code 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.