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

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

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 comment

1 answer

✓ Accepted answer

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.

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