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

Please explain for i, v in pairs() do?

Can someone explain me the use of this for loop? Your answer will be highly appreciated!

2 answers

✓ Accepted answer

a for i,v in pairs() do loop is a type of for loop often used for looping through tables.

An example is:

local myTable = {"my","table",5}
for i,v in pairs(myTable) do
	print(i,v)
end

The i,v is Index and Value, where the Index shows the number you're up to in the loop and the Value is the ValueType of what you're up to. Eg, if on the second go it would print out 2 table.

Another example:

local model = workspace.DoorModel
for i,v in pairs(model:GetDescendants()) do
	v.Transparency = .5 
end

This would change all the descendants of the DoorModel to have their transparency set to 0.5.

2 comments

0

Oh, got it!

0

-1. The iterator used doesn't make it a different type of loop.

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