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

Why doesn't it change BrickColor?

Asked by 8 years ago

Says nothing in Output log. Trying to change a Bunch of different parts colors.

local Co1 = "Navy blue"
local Co2 = "Lily white"

for i,v in pairs() do
if v:IsA("Part") and v.Name = Color1 then
v.BrickColor = BrickColor.new = (Co1)
end
end

for i,v in pairs() do
if v:IsA("Part") and v.Name = Color2 then
v.BrickColor = BrickColor.new =(Co2)
end
end

3 answers

Log in to vote
3
Answered by 8 years ago
local Co1 = "Navy blue"
local Co2 = "Lily white"

for i,v in pairs(game.Workspace:GetChildren()) do
    if v:IsA("Part") and v.Name == "Color1" then
        v.BrickColor == BrickColor.new(Co1)
    end
end

for i,v in pairs(game.Workspace:GetChildren()) do
    if v:IsA("Part") and v.Name == "Color2" then
        v.BrickColor == BrickColor.new(Co2)
    end
end

Their were 4 problems that I noticed.

Firstly, you were looking for v.Name, and so it was looking for a string. I put "" around Color1 and Color2 to make it a string.

Secondly, with BrickColor, you don't go BrickColor.new = (), as BrickColor is just BrickColor.new() and inside those brackets is where you have your color.

Thirdly, you only had 1 =. When comparing something, or checking it in an if statement, you must have ==

Finally, in the for loop, you weren't looking for anything. I am guessing you are checking the children of Workspace, so I used game.Workspace:GetChildren() to get the children of Workspace, and so then the script can continue running to look for a part with the certain name.

With that last little bit, if you ARE checking for parts in the Workspace, then you can make this code a lot shorter and less cluttered

local Co1 = "Navy blue"
local Co2 = "Lily white"

for i,v in pairs(game.Workspace:GetChildren()) do
    if v:IsA("Part") then
        if v.Name == "Color1" then
            v.BrickColor == BrickColor.new(Co1)
        elseif v.Name == "Color2" then
            v.BrickColor = BrickColor.new(Co2)
        end
    end
end

What I did was check the v:IsA("Part") first, as you don't need to repeat it. It then checks for the parts. Doing this is not technically necessary, but it just makes your code a lot easier to read and if you ever work with other developers, they can help edit your code, and they can see what's going on where.

Hope this helped and be sure to accept answer and +1

If their are any errors from this, please reply to this answer with anything in the output, or edit your answer with what you have and the output.

  • NinjoOnline
0
Doesnt work iSidersz 70 — 8y
0
23:40:39.799 - Workspace.Script:6: 'then' expected near '=' < Output iSidersz 70 — 8y
1
Add an = sign to the "if v.Name = "Color1" then", it should be "if v.Name == "Color1" then" sigve10 94 — 8y
0
Editted with what should work NinjoOnline 1146 — 8y
0
Works! iSidersz 70 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

You don't assign anything to the

for i,v in pairs() do

sections of the code.

A proper way to do this

local Co1 = "Navy blue"
local Co2 = "Lily white"

for i,v in pairs(game.Workspace.Model1:GetChildren()) do
if v:IsA("Part") and v.Name = Color1 then
v.BrickColor = BrickColor.new = (Co1)
end
end

for i,v in pairs(game.Workspace.Model2:GetChildren()) do
if v:IsA("Part") and v.Name = Color2 then
v.BrickColor = BrickColor.new =(Co2)
end
end

So it doesnt have to be a model as the name makes it seem. It can be anything a script, part, gui, etc.

How it works:

for i,v in pairs(game.Workspace.Model1:GetChildren()) do
end

This part takes all the children of the instance given. If there are no children of the instance it won't error because it did run. There was just no children to run the following code to.

Hope this helps :)

Log in to vote
-1
Answered by
ItsMeKlc 235 Moderation Voter
8 years ago

I believe the way you defined the variables confused it

local Co1 = BrickColor.new =("Navy blue")
local Co2 = BrickColor.new =("Lily white")

for i,v in pairs() do
if v:IsA("Part") and v.Name = Color1 then
v.BrickColor = Co1
end
end

for i,v in pairs() do
if v:IsA("Part") and v.Name = Color2 then
v.BrickColor = Co2
end
end

Answer this question