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

How and when would I use in pairs?

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

1 answer

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

3 comments

0

But I am confused about how would they know what it is

0

But I am confused about how would they know what it is

0

What do you mean?

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