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

How can I get my car to randomize all parts to the same color everytime the server is loaded?

Asked by 7 years ago

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?

1 answer

Log in to vote
1
Answered by 7 years ago

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
0
Alternatively, you could just change line 4 to "local paintRandom = math.random(1,2)", and remove line 7. Pyrondon 2089 — 7y
0
That is very clever either way, Thanks for the help. I am new to this but I have a general understanding. Dekadrachm 50 — 7y
0
That did it, I even added another car to it, although I had to add a wait of 0.1 because it didnt paint the cars anymore. Dekadrachm 50 — 7y
0
You can also use a BrickColorValue itsJooJoo 195 — 7y
0
Glad I could help :) chill22518 145 — 7y
Ad

Answer this question