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 9 years ago

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

01local Co1 = "Navy blue"
02local Co2 = "Lily white"
03 
04for i,v in pairs() do
05if v:IsA("Part") and v.Name = Color1 then
06v.BrickColor = BrickColor.new = (Co1)
07end
08end
09 
10for i,v in pairs() do
11if v:IsA("Part") and v.Name = Color2 then
12v.BrickColor = BrickColor.new =(Co2)
13end
14end

3 answers

Log in to vote
3
Answered by 9 years ago
01local Co1 = "Navy blue"
02local Co2 = "Lily white"
03 
04for i,v in pairs(game.Workspace:GetChildren()) do
05    if v:IsA("Part") and v.Name == "Color1" then
06        v.BrickColor == BrickColor.new(Co1)
07    end
08end
09 
10for i,v in pairs(game.Workspace:GetChildren()) do
11    if v:IsA("Part") and v.Name == "Color2" then
12        v.BrickColor == BrickColor.new(Co2)
13    end
14end

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

01local Co1 = "Navy blue"
02local Co2 = "Lily white"
03 
04for i,v in pairs(game.Workspace:GetChildren()) do
05    if v:IsA("Part") then
06        if v.Name == "Color1" then
07            v.BrickColor == BrickColor.new(Co1)
08        elseif v.Name == "Color2" then
09            v.BrickColor = BrickColor.new(Co2)
10        end
11    end
12end

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

You don't assign anything to the

1for i,v in pairs() do

sections of the code.

A proper way to do this

01local Co1 = "Navy blue"
02local Co2 = "Lily white"
03 
04for i,v in pairs(game.Workspace.Model1:GetChildren()) do
05if v:IsA("Part") and v.Name = Color1 then
06v.BrickColor = BrickColor.new = (Co1)
07end
08end
09 
10for i,v in pairs(game.Workspace.Model2:GetChildren()) do
11if v:IsA("Part") and v.Name = Color2 then
12v.BrickColor = BrickColor.new =(Co2)
13end
14end

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:

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

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
9 years ago

I believe the way you defined the variables confused it

01local Co1 = BrickColor.new =("Navy blue")
02local Co2 = BrickColor.new =("Lily white")
03 
04for i,v in pairs() do
05if v:IsA("Part") and v.Name = Color1 then
06v.BrickColor = Co1
07end
08end
09 
10for i,v in pairs() do
11if v:IsA("Part") and v.Name = Color2 then
12v.BrickColor = Co2
13end
14end

Answer this question