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

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

Asked by 4 years ago

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

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
Oh, got it! SilentsReplacement 468 — 4y
0
-1. The iterator used doesn't make it a different type of loop. hiimgoodpack 2009 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

Here's a great post that relates to your question.

In the answers section, there are many examples provided that explain what pairs does, as well as ipairs and next.

Answer this question