Just made another Chance System, not sure if yall need another one but made it anyway since I was making one for my game.
Here’s the code:
Chance System
Very straightforward and easy to use. All you need to use is two methods inside the module:
system:RefinePercentages()
and
system:AppointChance()
The first method you need to run is RefinePercentages. This takes in two tables, the first table being the different rarities and the percentage of getting that certain rarity out of a hundred, like so:
local Rarities={ --You can add as many rarities as you'd like, just make sure they all add up to a 100.
['Basic']=40; --Remember, these are percentages to roll the rarity out of 100%.
['Rare']=30;
['Epic']=20;
['Legendary']=10;
}
The second table is the actual results that you can gain from the Rarity that is rolled.
For example:
local PossibleResults={--YOU CAN HAVE INFINITE POSSIBLE RESULTS, MATCH them to the RARITIES
['Result1']='Basic';
['Result2']='Basic';
['Result3']='Rare';
['Result4']='Rare';
['Result5']='Epic';
['Result6']='Epic';
['Result7']='Legendary';
['Result8']='Legendary';
}
Now we have our two tables, we will combine these tables using the :RefinePercentages() method, like so:
local System=require(PathToChanceSystem)
local Rarities={
['Basic']=40;
['Rare']=30;
['Epic']=20;
['Legendary']=10;
}
local PossibleResults={
['Result1']='Basic';
['Result2']='Basic';
['Result3']='Rare';
['Result4']='Rare';
['Result5']='Epic';
['Result6']='Epic';
['Result7']='Legendary';
['Result8']='Legendary';
}
local RefinedPercentageTable=System:RefinePercentages(Rarities, PossibleResults)
This refine method will give us a new table, which lists the ACTUAL chance of pulling the certain item
Next, we will run this table we got through the :AppointChance() method:
local RefinedPercentageTable=System:RefinePercentages(Rarities, PossibleResults)
local FinalResult=System:AppointChance(RefinedPercentageTable)
And the FinalResult will be the item you pulled.
Combined Code:
local System=require(PathToChanceSystem)
local Rarities={
['Basic']=40;
['Rare']=30;
['Epic']=20;
['Legendary']=10;
}
local PossibleResults={
['Result1']='Basic';
['Result2']='Basic';
['Result3']='Rare';
['Result4']='Rare';
['Result5']='Epic';
['Result6']='Epic';
['Result7']='Legendary';
['Result8']='Legendary';
}
local RefinedPercentageTable=System:RefinePercentages(Rarities, PossibleResults)
local FinalResult=System:AppointChance(RefinedPercentageTable)
print(FinalResult)
Thank yall for reading!