Detecting if parts in a model are actually parts or not gives an error

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.

image

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.

1 Like

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.

3 Likes

I changed it to this

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

But it didn’t color the MeshPart to be Lime Green

if v:IsA("Part") then
		v.BrickColor = BrickColor.new("Really red")
	elseif v:IsA("MeshPart") then
		v.BrickColor = BrickColor.new("Lime Green")
	end
2 Likes

How can I make it so I can keep the script inside the model and make it so the script ignores itself and only detects the other parts in the model?

1 Like
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
1 Like

this addition is not necessary

if v:IsA("Part") then
	v.BrickColor = BrickColor.new("Really red")
elseif v:IsA("MeshPart") then
	v.BrickColor = BrickColor.new("Lime Green")
end

is enough

1 Like

He wanted it to be in the model itself, I also put this, but he wants the script in the model.

1 Like

That doesnt change anything though

1 Like

Doesn’t really matter anymore, he just asked, maybe he wants to add more logic, but I don’t know.

1 Like