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

How to replace every red part with blue?

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

1
Capitalize ClassName, Lua is case sensitive. M39a9am3R 3210 — 10y

2 answers

Log in to vote
3
Answered by
Defaultio 160
10 years ago

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!

Ad
Log in to vote
-1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago

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
0
Doesn't work. ultimate055 150 — 10y

Answer this question