How to make a luck boost system explained

If you came this far that means that you already know about weighted chance system and now dont have a damn clue how to make it support luck boost, well im gonna explain it as detailed as I can.
First off all rng system have tables that stores chances and the most popular method is weighted chance system which woud look something like this

local Drops = { 
["Sword"] = {Weight = 20};
["Backpack"] = {Weight = 5};
-- and so on
}
return Drops

Well for this tutorial we will be using reversed logic, we are gonna store our chances as
1/x

local Drops = {
["Ruby"] =  {Chance = 100};
["Gold Bar"] = {Chance = 10};
["Sword"] = {Chance = 1};
}
return Drops

Its VERY important to start from the rarest item for this to work, you will see why later.
Alright, now that we have our table we want to write a function which will do the rolling with luck boost, we will be using math.random() without arguments which will return a number between 0 and 1

0.065847105299656
0.65157980629391
0.60489170235075
0.58331046966314
0.44356169060376
0.093403114018018
0.45316910777516
0.49862120910204
0.34194418829838
0.39400618165677
0.040796727393246
0.49823202763717
0.14416463649512
0.69286250736329
0.96278283079283
0.18328742356498
0.11146662761014
--example from luau playground after running some math.random() :d

and after that as always we are gonna loop trought our table and implement our logic

function Roll(luckBoost)-- lets say its 10 for now, the loop will start from ruby
local rng = math.random()
for Name, Table in Drops do

local modifiedChance = Table.Chance

if Table.Chance > 1 then
modifiedChance = Table.Chance/luckBoost -- Ruby becomes 10, Gold 1 and Sword stays 1 in 1
end

if modifiedChance <= 1 then -- On first loop Ruby is skipped, On the second loop gold would get picked since its now 1 in 1
return Name
end 

if rng <= (1/modifiedChance) then return Name end -- if roll is lower than 1/modifiedChance 
--which in this case is 0.1 for ruby and for the first number of math.random() to be 0.0
--


end
end

so yeh in summary we are just dividing our chance by luck boost and checking if guaranteed (<= 1),
if its not guaranteed then we turn modified chance into a deciaml and check if roll is lower, the rarer the item, the lower deciaml we will get which will be rarer for math.random() to be lower or equal than that
edit: you will need to return the worst item at the end if lowest chance is 2 atleast and you dont roll anything

6 Likes