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

How and when would I use in pairs?

Asked by 4 years ago

I am new to scripting, can someone help me here?

1 answer

Log in to vote
0
Answered by
kisty1 111
4 years ago

You use pairs for iterating over an array or a dictionary For example

local t = {1, 2, 3, 4, test = 5}
for i, v in pairs(t)
    print(i, v)
end

This will print all values in the table along with their indexes You can also use next for doing the exact same thing

local t = {1, 2, 3, 4, test = 5}
for i, v in next, t do
    print(v)
end

Lets say you want to change the color of all parts in a model You can use the GetDescendants method to get everything in the model and then check if the thing is a part, if it is a part, set its color to whatever you desire

for i, part in pairs(model:GetDescendants()) do
    -- check if the descendant is a part
    if part:IsA("BasePart") then
        -- change its color to white
        part.Color = Color3.fromRGB(255, 255, 255)
    end
end

It is possible to substitute pairs with next

0
But I am confused about how would they know what it is PrismaticFruits 842 — 4y
0
But I am confused about how would they know what it is PrismaticFruits 842 — 4y
0
What do you mean? kisty1 111 — 4y
Ad

Answer this question