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

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

Asked by 8 years ago

What do you need to do ?

A for loop ? math.random() ?

Some help on here please.

0
`math.random(1,chance)` would be your best bet. ChemicalHex 979 — 8y

3 answers

Log in to vote
2
Answered by 8 years ago

math.random()

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

if math.random() <= 1/5 then
  -- Boom, golden brick!
end
Ad
Log in to vote
0
Answered by
iNicklas 215 Moderation Voter
8 years ago

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

if math.random(1, 100) <= script.Chance.Value then
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. XAXA 1569 — 8y
Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
8 years ago

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

Answer this question