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.
How would I add weights such as 1 in 10000? would this be 0.0001?
Right. So you donât have to use 0-1; you could use whole numbers instead, like 10000. Keep in mind, though, that if numbers get too big, theyâll start turning into scientific notation, so make sure the total weight doesnât exceed the 64-bit limit
Edit: Yes, you could use 0.0001. Keep in mind that the total weight has to be greater than 1; otherwise, math.random
canât return a value between 0 and 1.
sorry for the late response
How would I do if I wanted to add luck boost? Like a 10% luck boost potion? Will it work if I add 10 to all chances? (If something have 1/100 chance of happening, and another 99/100, if I add 100 to all entries I will be left with 101/300 and 199/300 which is roughly 33.66% and 66.33%)
You could add a percentage to all chances because, ultimately, it would increase the likelihood that math.random would choose that number. Alternatively, you could just distribute the luck boost from the rarest to the least rarest accordingly.
what? if you went through everyitem and multiplied it by said luck it would not make a difference
lets say you had 2 items
common = 100
uncommon = 1
luck = 2
common * luck (100 * 2) = 200
uncommon * luck (1 * 2) = 2
1 / 100 = 0.01
2 / 200 = 0.01
wth thats some complicated math for my adhd ahh brain
edit: iâve eventually got used to it and now it doesnât seem like THAT hard of a thing to me anymore, this post explains it really well.
There is no need to use ipairs at all here since there wonât be any nil value. Also the string interpolation is just your preferece.
do i put this in a module script or a normal server script