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

How would you make a randomized chance to spawn for example a golden brick?

What do you need to do ?

A for loop ? math.random() ?

Some help on here please.

1 comment

0

math.random(1,chance) would be your best bet.

3 answers

✓ Accepted answer

math.random()

If you wanted it to be, say, a 1 in 5 chance:

if math.random() <= 1/5 then
  -- Boom, golden brick!
end

I mean, I myself would use Int Values and math.random

if math.random(1, 100) <= script.Chance.Value then

1 comment

0

Will not quite work as expected if Chance is a decimal value. math.random generates integers. If chance was set to 1.99 (1.99%), it would effectively have a 1% probability of occurring. Furthermore, chances under 1% will always have a 0% probability of occurring.

Another way of solving this is to have some sort of rarity variable, where a golden brick has a 1 in rarity chance of appearing. Making the rarity of the brick bigger means that it is more rare!

local GOLDEN_BRICK_RARITY = 100 -- this means that 1 in 100 bricks spawned will be golden. Setting this to 1 means a golden brick will ALWAYS spawn.

function SpawnBrick()
	if math.random(1, GOLDEN_BRICK_RARITY) == 1 then
		-- TODO: Spawn a golden brick
	else
		-- TODO: Spawn a regular brick.
end

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