Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

3 answers

✓ Accepted answer

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!

1 comment

0

Thanks

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

2 comments

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.

0

Oh thanks

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.