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

What does "for i,v in pairs" mean?I do not know how to use them.

Asked by 6 years ago

I have seen this quite a few times but I do not know what it means.Any help?

3 answers

Log in to vote
0
Answered by
popeeyy 493 Moderation Voter
6 years ago

i,v in pairs is used for sorting through a table. For example:

local tab = {'help'}

for position,value in pairs (tab) do
    print('The value in the table, '..value..' is in position '..tostring(position))
end

This script shows all the values in the table and their position.

When you do :GetChildren() on an object, it returns a table of the children. To sort through the children of an object, you would do this:

for position,child in pairs (workspace:GetChildren()) do
    print('There is an object in workspace named '..child.Name..' and a position of '..tostring(position)
end

You can change position and child to any variable you desire, for example, i and v.

I hope this helps you!

0
Thanks rochel89 42 — 6y
Ad
Log in to vote
0
Answered by
aindev 4
6 years ago

When using for i,v in pairs, it can get all children in any object.

For Example:

for i,v in pairs(game.Workspace:GetChildren()) do
    v.Transparency = 0.5
end
0
No, i, v in pairs does not get the children. It iterates through the children. What gets the children of any object is GetChildren() itself. GetGlobals 343 — 6y
0
Oh thanks rochel89 42 — 6y
Log in to vote
0
Answered by 6 years ago

Basically, this iterates through the children of an instance given in the parameters.

for i, v in pairs(Example:GetChildren()) do
    --code
end

The first value, i stands for index, and is essentially which iteration it is currently on and v is the current instance. For example, let's say Example is a model with 3 parts in it, and i wanted to make them all transparent:

for i, v in pairs(Example:GetChildren()) do
    v.Transparency = 1
    print(i) --This will output which part we are currently 'editing'
end

Each brick will become transparent, and for each one it will print respectively, 1, 2 and 3. For a more detailed answer, go to the link aazkao helpfully provided for you.

Answer this question