Says nothing in Output log. Trying to change a Bunch of different parts colors.
01 | local Co 1 = "Navy blue" |
02 | local Co 2 = "Lily white" |
03 |
04 | for i,v in pairs () do |
05 | if v:IsA( "Part" ) and v.Name = Color 1 then |
06 | v.BrickColor = BrickColor.new = (Co 1 ) |
07 | end |
08 | end |
09 |
10 | for i,v in pairs () do |
11 | if v:IsA( "Part" ) and v.Name = Color 2 then |
12 | v.BrickColor = BrickColor.new = (Co 2 ) |
13 | end |
14 | end |
01 | local Co 1 = "Navy blue" |
02 | local Co 2 = "Lily white" |
03 |
04 | for i,v in pairs (game.Workspace:GetChildren()) do |
05 | if v:IsA( "Part" ) and v.Name = = "Color1" then |
06 | v.BrickColor = = BrickColor.new(Co 1 ) |
07 | end |
08 | end |
09 |
10 | for i,v in pairs (game.Workspace:GetChildren()) do |
11 | if v:IsA( "Part" ) and v.Name = = "Color2" then |
12 | v.BrickColor = = BrickColor.new(Co 2 ) |
13 | end |
14 | 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
01 | local Co 1 = "Navy blue" |
02 | local Co 2 = "Lily white" |
03 |
04 | for 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(Co 1 ) |
08 | elseif v.Name = = "Color2" then |
09 | v.BrickColor = BrickColor.new(Co 2 ) |
10 | end |
11 | end |
12 | 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.
You don't assign anything to the
1 | for i,v in pairs () do |
sections of the code.
A proper way to do this
01 | local Co 1 = "Navy blue" |
02 | local Co 2 = "Lily white" |
03 |
04 | for i,v in pairs (game.Workspace.Model 1 :GetChildren()) do |
05 | if v:IsA( "Part" ) and v.Name = Color 1 then |
06 | v.BrickColor = BrickColor.new = (Co 1 ) |
07 | end |
08 | end |
09 |
10 | for i,v in pairs (game.Workspace.Model 2 :GetChildren()) do |
11 | if v:IsA( "Part" ) and v.Name = Color 2 then |
12 | v.BrickColor = BrickColor.new = (Co 2 ) |
13 | end |
14 | 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:
1 | for i,v in pairs (game.Workspace.Model 1 :GetChildren()) do |
2 | 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 :)
I believe the way you defined the variables confused it
01 | local Co 1 = BrickColor.new = ( "Navy blue" ) |
02 | local Co 2 = BrickColor.new = ( "Lily white" ) |
03 |
04 | for i,v in pairs () do |
05 | if v:IsA( "Part" ) and v.Name = Color 1 then |
06 | v.BrickColor = Co 1 |
07 | end |
08 | end |
09 |
10 | for i,v in pairs () do |
11 | if v:IsA( "Part" ) and v.Name = Color 2 then |
12 | v.BrickColor = Co 2 |
13 | end |
14 | end |