I am new to the Developer Forum, so I don’t know if I am doing something wrong. But I made a model with many parts in it, and I tried to use a script inside of the model to make it so that it detects if the parts inside the model are actually Parts or not.
local parts = script.Parent:GetChildren()
for i,v in pairs(parts) do
if v:IsA("Part") then
v.BrickColor = BrickColor.new("Really red")
else
v.BrickColor = BrickColor.new("Lime Green")
end
end
I added a MeshPart to test if it will turn lime green because it is not a Part. But upon testing the game I get this error:
BrickColor is not a valid member of Script “Workspace.Parts.Script”
I don’t know how to fix this, I appreciate if someone could help.
Remove pairs as it’s not necessary here. and in the if statement it’s recommended to use BasePart as it detects also meshes.
But the main issue is in the else statement, the if statement detects if the value is a part, but I see you have a script inside the model, so its detecting the script, but the script doesn’t have a property named BrickColor, so move the script out the model, or add more type checking in the loop.
local parts = game.Workspace.Parts:GetChildren()
for i,v in (parts) do
if v:IsA("Part") then
v.BrickColor = BrickColor.new("Really red")
else
v.BrickColor = BrickColor.new("Lime Green")
end
end
local parts = game.Workspace.Parts:GetChildren()
for i,v in (parts) do
if v:IsA("Part") then
v.BrickColor = BrickColor.new("Really red")
elseif v:IsA("MeshPart") then
v.BrickColor = BrickColor.new("Lime Green")
elseif v:IsA("Script") or v.Name == "Script" then
continue
end
end