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

How to change BrickColor of all children of Workspace?

Asked by 7 years ago
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.

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. User#14829 0 — 7y

1 answer

Log in to vote
1
Answered by
cabbler 1942 Moderation Voter
7 years ago
Edited 7 years ago

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)
0
Thank you, but it doesn't seem to be working... It's not giving an error, but the grass is still green. Speedracerpro -4 — 7y
0
lol run it as a command, it works. There are problems with your script. cabbler 1942 — 7y
0
ohhh that works! Ty Speedracerpro -4 — 7y
Ad

Answer this question