How do I make a randomized SurfaceGui BackgroundColor3?

I’m making a subway train that has no actual line it’s assigned to and I want to get the destination board up front to be random/other lines beside what’s displayed below in the snapshot, such as red, green, brown, pink, and purple. Right now, it’s at the default, which is a blue background. I don’t want the text to change.

Subway Front:

I’m not that good of a scripter and I know there’s an easy way to do this, but I just don’t know what. I’ve tried math.random() but my code always has an error in the output. My goal is to get the train to randomize the color only once and then the script deletes itself via :Destroy(). Since it’s going to be spawned more than once, I want it to vary every time.

Here’s a snapshot of my broken script I tried:

local colors = {
	Color3.fromRGB(0, 100, 200), -- Blue
	Color3.fromRGB(200, 0, 0), -- Red
	Color3.fromRGB(0, 200, 0), -- Green
}

script.Parent.Board.Board.BackgroundColor3 = colors[math.random(1, #colors)]
wait()
script:Destroy()
2 Likes

Seems to work fine for me, but the script was disabled in your model?

Oh dear, that probably explains it. I forgot to enable it. :roll_eyes:

Thanks.

1 Like

Another way of doing this would be:

SurfaceGui.Frame.BackgroundColor3 = BrickColor.random().Color

Doesn’t let you have complete control of colours though, instead picks a random one from the brickcolor pallette.

That won’t work because there’s a specific set of subway lines that exist in Chicago’s subway system.

I was going to suggest this, but it’s only limited to the BrickColor palette.

You could try:

Thing.BackgroundColor3 = Color3.new(math.random(1,255),math.random(1,255),math.random(1,255))

That won’t work, Color3.new expects values between 0 and 1 for each component. In other words Color3.new(math.random(), math.random(), math.random()). Note that randomizing the components is not really going to yield any nice color results most of the time.

Instead if Color3.new you would use Color3.fromRGB in your method.

Then the best thing I could think of is @Osyris 's method with the table or colors.