my bad, they indeed do not work with dictionaries
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
Would this work for rarities that are like 1 in a trillion?
Not sure. The numbers could convert to scientific notation and mess up the rarities, if it does, try using some big numbers module.