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

How to change BrickColor of all children of Workspace?

for i,v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "Part" then
		if v.Material == "Grass" then
			v.BrickColor = BrickColor.new("Quail grey")
		else
			print("Not Grass")
		end
	end
end

That's my current code, but unfortunately, not all children of Workspace have a material property so it gives an error like "Material is not a valid member of Camera" or something like that and stops working. I also have models in it, and scripts, etc.

1 comment

0

What I would do is create a recursive function (pretty sure there's a more efficient way) in order to retrieve all parts in workspace (stored in table, do :IsA("BasePart") to check if the object is a part), then loop through the table. Rest is self explanatory.

1 answer

✓ Accepted answer

Instead of checking the name, check if v:IsA('BasePart'). This includes parts, wedge, truss, union, etc.

Also Material is an Enum not a string.

To scan literally every object in workspace, do a recursive function:

function scan(x)
	for _,v in pairs(x:GetChildren()) do
	    if v:IsA('BasePart') then
	        if v.Material == Enum.Material.Grass then
	            v.BrickColor = BrickColor.new("Quail grey")
	        else
	            print("Not Grass")
	        end
	    end
	    scan(v)
	end
end
scan(workspace)

3 comments

0

Thank you, but it doesn't seem to be working... It's not giving an error, but the grass is still green.

0

lol run it as a command, it works. There are problems with your script.

0

ohhh that works! Ty

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