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

Obtaining a bool value based on percentage chance?

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?

2 comments

0

I personally wouldn't want to use percentages. I would try a workaround but i am not sure what you would be using this for. If you are making a type of item drop table when a monster dies, you could use math.random with > or < for different items. This can work with alot of things. Like say on math.random 1 out of 100, you could do <= 10 that can be like 10%, not sure this helps, cant put code now

0

It's probability - percentages/fractions/decimals are all equivalent and the concept of weighting the chance is vital

1 answer

✓ Accepted answer

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!

1 comment

0

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()

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