Say I have variables set up like so:
local Chance = IntValue.Value -- An integer from 0 to 100 local Outcome = false
Now the goal here is to change the value of Outcome
to true
based on whatever Chance
is, where the probability that Outcome
becomes true
is Chance/100
.
For example: Chance = 3
so the probability that Outcome
becomes true
is 0.03 (or 3%).
How would I even go about doing this?
This is one of my favourite topics:
local Randomness = Random.new() local Chance = 10 local Outcome = false local function runChance() if Randomness:NextNumber() <= Chance/100 then Outcome = true else Outcome = false end end runChance()
How this works:
NextNumber()
gives us a number from 0 to 1 in decimals. Any chance divided by 100 will give us what we want; so 10 percent chance would also be 10/100 which is 0.1 so what we are saying is if this random number is smaller then the chance then it works.
So if you have 100 as your chance; 100 / 100 = 1, :NextNumber()
will always generate a number at 1 or below 1. The higher the number, the higher the chance basically
Hope this helped! Best of luck!