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

What is the Use of _,i in pairs? [closed]

Asked by
woodengop 1134 Moderation Voter
9 years ago

This question already has an answer here:

What does "in pairs" do?

What is the use of

_,i in pairs

I have always heard that term and I do not know what it's Purpose, and its ability.

0
Duplicate. Remember to use the search bar. Lacryma 548 — 9y

Marked as Duplicate by Lacryma, EzraNehemiah_TF2, and evaera

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
2
Answered by
Diitto 230 Moderation Voter
9 years ago

It is used to iterate a table, or go through every single value inside the table. I personally do not use it, choosing instead to use next(it works a different way), but it depends on your style.

Example:


for Index,Object in pairs(Workspace:GetChildren())do print(string.format( '%q is the %dth child of Workspace.', tostring(Object), Index )); end;

Workspace:GetChildren() returns an array of instances that are direct descendants of Workspace.

Index is the key that is held in direct linkage to the table, with Object being the value. In this case, Object is the instance that's parent is Workspace.

Another Example:


local Friends={ ['xxProMLGxx'] = 'a l33t hax0r', ['TheContinentOfAmerica'] = 'your brother', ['Shedletsky'] = 'the ROBLOX Creative Director', }; for Username, Description in ipairs(Friends) do print(Username..' is '..Description..', and is a friend of yours, no?'); end;

The Friends array was a dictionary that's keys were the usernames of some players and values were their descriptions.

Username was the index, which was the player's username, and Description was the phrase associated with the username.

0
xD Thanks bro. woodengop 1134 — 9y
0
What's the difference between "next" and "pairs"? Vlatkovski 320 — 9y
Ad