Weighted Chance System

my bad, they indeed do not work with dictionaries

1 Like

I was the same guy who posted that, but @Artzified answered your question, and it doesn’t match my current scripting methods, but I kept it for archival reasons and it really isn’t an upgrade like what @acuaro said, minor changes are done.


This is a great system of a Weighted Chance System, but I recommend putting the code in code blocks instead of images, so you can copy them. and here is some things you can do in the code, instead of writing

'Congrats you won '.. Prize

You can write

'Congrats you won', Prize

and you don’t need to put 1 as the first argument in math.random, as you can just write it like math.random(Weight), You don’t even need to use pairs or ipairs to loop through a dictionairy, as you can just write

for Prize, Chance in PrizeTable do

and you do not need to write the table indexes like ["name"] unless it contains a character which writing it normally doesn’t support, like this

local PrizeTable = {
   Prize_1 = 0.1, -- works fine
   ["Prize_2"] = 0.6, -- also does the same result as the line above
   -- but It's required for table indexes with characters
   -- that isn't normally supported
}

also sorry if I put multiline comments, so people don’t have to scroll through the code

They both do the same results

1 Like

Would this work for rarities that are like 1 in a trillion?

1 Like

Not sure. The numbers could convert to scientific notation and mess up the rarities, if it does, try using some big numbers module.