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

How to use math.random with brick colors? (ANSWERED)

Asked by 9 years ago

I wanted to make a script so alot of different colored bricks come out of the ground but i dont know how to do math.random for brick colors i thought it was like this:

p.BrickColor = BrickColor.new(math.random(1,5), math.random(1,5), math.random(1,5))

Is that correct or no?

2 answers

Log in to vote
1
Answered by
W8X 95
9 years ago

Use this

p.BrickColor = BrickColor.random()
0
If the answer is right, remember to press accept answer. W8X 95 — 9y
0
Ok PixelZombieX 8 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

You can use BrickColor.Random(), but note that it chooses from the available BrickColors; on average, it returns an RGB of (0.59, 0.56, 0.53), whereas a truly random colour generator should return an average of about (0.5, 0.5, 0.5). In other words, you'll be more likely to get a colour with a lot of red in it than you might expect (though it's unlikely you'd notice in practice).

If you want a more even distribution, use:

p.BrickColor = BrickColor.new(math.random(), math.random(), math.random())

The constructor is BrickColor.new(r,g,b), where r,g,b are between 0 and 1. math.random() without parameters gives such a number (though math.random() will never return precisely 1; it might return 0.999999 instead).

Using this method isn't perfect either, since the available BrickColors are not evenly distributed, but it's much closer, giving an average RGB of (0.50, 0.49, 0.50).

See http://wiki.roblox.com/index.php?title=API:BrickColor for more on BrickColor, including other ways of creating one.

Answer this question