Changing Parts in Pairs

I’m looking to create a script which changes all the trees color in game, so I don’t have to alter each part named “Leaves.” To be done for Autumn, Winter and Spring. I know how to do the random tree colours, but the Issue is that everything that is a mesh part in the workspace seems to get changed as well. I tried looking for another parameter than "if _:IsA" but I couldn’t find anything.

local TreesColour = workspace:GetDescendants("Leaves")

for i,_ in pairs (TreesColour) do
	if _:IsA ("MeshPart") then
		print("Tree Color Changed")
		_.Color = Color3.fromRGB(255, 213, 128)
	end
end

If anyone can give some help thanks?

I’m not really sure what you’re trying to do. Instance:GetDescendants doesn’t take any parameters, are you trying to change it only on parts whose names are “Leaves”?

If so, you would just check if _.Name == “Leaves”

Essentially I’m trying to change everything name “Leaves” from the workspace to a different color, no matter if in model or folder.

And thanks, I thought It was a lot more complicated than that, it worked.

1 Like
for _,leaf in pairs(workspace:GetDescendants()) do
  if leaf:IsA("MeshPart") and leaf.Name == "Leaves" then
    print("Tree Color Changed")
    leaf.Color = Color3.fromRGB(255, 213, 128)
  end
end

should work

2 Likes

Please don’t use _ as a variable name. Its purpose is to be a placeholder name for a variable you are discarding. It would make sense to do:

for _, part in pairs (TreesColour) do

Since you don’t use the first return value of pairs(), which is the intended use of _

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.