How to make chance based events

Hello! I was wondering, how could I make chance based events? (don’t get me wrong, I’m not asking for full scripts).

An example would be that you have a lamp in like a simulator/tycoon type game. There is a certain chance that the lamp will break or that the electricity will cut out, meaning that the lamp will stop shining. If you purchase a backup electricity generator, the chance will decrease.

I was thinking about certain ways, such as math.random(percentageChance, 100), which then would return either true or false values (break the lamp/not break the lamp), but I’m not really sure about that.

local accuracy = 10 -- i like to add this to control how many decimals the percentage has

local randomNumber = math.random(0,100 * accuracy) 

local chance = 33.3

if randomNumber < chance * accuracy then
    print("you are lucky")
end
1 Like

Oh and if you do decide to add accuracy, then each power of 10 is a new decimal, so 10 allows 1 decimal, 100 allows 2 decimal etc

Thanks, looks good and should work how I’d expect it to work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.