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

How does the in pairs function work? [closed]

Asked by 8 years ago

I never really understood the in pairs function, and as I get deeper into Lua, I feel like it is really useful, but I just can't understand it, can anyone help me?

0
It loops through the values of a table. User#11440 120 — 8y
0
Yeah, but what do you mean by that fabiotheone00 -2 — 8y
View all comments (4 more)

Marked as Duplicate by Perci1

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
1
Answered by 8 years ago

How in pairs works is that unlike a normal for loop which iterates a certain amount, it iterates through a table/array.

So let's say I have an array with five values, I would call on it like so:

local mytable = {54, 3, 9, 10}

for i, v in pairs(mytable) do
    print(v)
end

The loop would print all of the values. Generic for loops like the one above are useful for iterating through an object's children effectively.

If this helped you, don't forget to accept the answer :)

Ad