Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

5 comments

0

Alternatively, you could just change line 4 to "local paintRandom = math.random(1,2)", and remove line 7.

0

That is very clever either way, Thanks for the help. I am new to this but I have a general understanding.

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.

0

You can also use a BrickColorValue

0

Glad I could help :)

This discussion is preserved as it was. New answers, comments, and votes are closed.