msg = game.Workspace:GetChildren()
for i = 1, #msg do
if (msg[i].className == "Part") then
if msg[i].BrickColor == ("Bright red") then
msg[i].BrickColor = ("Bright blue")
end
end
end
It's not working right.
msg = game.Workspace:GetChildren()
for i = 1, #msg do
if (msg[i].className == "Part") then
if msg[i].BrickColor == ("Bright red") then
msg[i].BrickColor = ("Bright blue")
end
end
end
It's not working right.
Your main problem is that you're trying to compare a BrickColor type to a string, "Bright red". This will always return false because the brick color "Bright red" is not equal to the words "Bright red". Two totally different things. To make it a brick color instead of a string, make it BrickColor.new("__") So you'd have, if msg[i].BrickColor==BrickColor.new("Bright red") then
But I would also like to suggest two changes to your code that might make it work a little bit better for you. First of all, line 3. If you only check if the brick is of className "Part", you're only going to get all brick parts. What about Wedges, CornerWedges, or seats? Instead, you can do if msg[i]:IsA("BasePart") then This will cover all types of parts.
The second potential issue is that this code only covers all bricks that are direct children of Workspace. What if a part is in a model? It's possible that you in fact only want to cover direct descendants of Workspace, but if you do want to get models, let me give you a tip for that. It's all about a little trick called recursion. Here's the code-
function colorRedsBlue(parent)
local msg = parent:GetChildren()
for i = 1, #msg do
if (msg[i]:IsA("BasePart")) then
if msg[i].BrickColor ==BrickColor.new("Bright red") then
msg[i].BrickColor = BrickColor.new("Bright blue")
end
elseif (msg[i]:IsA("Model")) then
colorRedsBlue(msg[i])
end
end
end
colorRedsBlue(game.Workspace)
So when you call the function colorRedsBlue for Workspace, it will run your code to color all the red parts blue. However, there's a new two lines added on, there. If what it finds is not a part, but it's a model, it will actually call the function again for the model. The function calls itself. This is the recursive step. The function will execute again and check all the parts in the model, and if there are any models in that model, it will continue on.
Hope this helps!
Lua is case Sensitive, capitalize ClassName
msg = game.Workspace:GetChildren()
for i = 1, #msg do
if (msg[i].ClassName == "Part") then
if msg[i].BrickColor == ("Bright red") then
msg[i].BrickColor = ("Bright blue")
end
end
end
Doesn't work.
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
Capitalize ClassName, Lua is case sensitive.