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

How to get a random color from 3 colors in a brick?

Asked by
oliras3 15
9 years ago

I have 3 colors and I want one of them into a block and it has to be random, first I used color3 but I only want blue, green or red. This is how it looks right now: script.Parent.Touched:connect(function(hit) if hit then hit.BrickColor = BrickColor.new end)

I dont know how to use math.random so I am looking for help here

1 answer

Log in to vote
1
Answered by
MunimR 125
9 years ago

Here is my approach to this problem:

local Colors = {"Bright red", "Bright blue", "Bright green"}

script.Parent.Touched:connect(function(otherPart)
    if otherPart then
        otherPart.BrickColor = BrickColor.new(Colors[math.random(1,3)])
    end
end)

Basically how this works is that we first get the desired colors into a table. Then we wait for the part to fire the touched event, and if the other part exists then we want to change it's BrickColor to a random color to a color from out table. We do this by first creating the new BrickColor then we choose a random index from our table. Resulting in a random color every time.

Ad

Answer this question