I've created I script which was intended to change the color of a car randomly every time the server was loaded, It worked... sorta. Instead of it changing the parts on a car to a solid color, I got a mixture of the two colors I allowed in my script, Yellow and White. Some of the car is Yellow and some White. I'm assuming it's because It's rolling the random function each time a child with a name "part" is found.
local sportscar2color = game.workspace.Cars.Sportscar2:GetChildren() paintRandom = nil function PaintCar(part) paintRandom = math.random(1,2) if paintRandom == 1 then part.BrickColor = BrickColor.White() elseif paintRandom == 2 then part.BrickColor = BrickColor.Yellow() end end for _, child in ipairs(sportscar2color) do if child.Name == "Part" then PaintCar(child) end end
How can I get my car to spawn and change all parts to the same randomly picked color?
That's because you are repeating the function for each part. So you have to store the color somehow.
How I would do it is I would create a part and call it for example Test, and store the color in it.
local Test = game.workspace.Test -- This refers to the part I created local sportscar2color = game.workspace.Cars.Sportscar2:GetChildren() paintRandom = nil function PaintCar(part) paintRandom = math.random(1,2) if paintRandom == 1 then part.BrickColor = BrickColor.White() elseif paintRandom == 2 then part.BrickColor = BrickColor.Yellow() end end PaintCar(Test) -- Calls the function on the part for _, child in ipairs(sportscar2color) do if child.Name == "Part" then child.BrickColor = Test.BrickColor -- This should copy the color from Test to the car part end end