First, make two tables containing your values, One for percentages, and another for rewards.
example:
local ChanceTable = {
["Common"] = 95,
["Uncommon"] = 85,
["Rare"] = 75,
["Epic"] = 25,
["Legendary"] = 10,
}
local RewardTable = {
["Common"] = 500,
["Uncommon"] = 1000,
["Rare"] = 2500,
["Epic"] = 6000,
["Legendary"] = 10000,
}
This will make it easier to add more rarities in the future.
Now, make a random number generator that goes from 0 to 100 like this:
local RNG = math.random(0,100)
Next, make a loop and a rarity variable. This will take your random number and scroll through the rarities, just make sure you have them in descending order (highest to lowest) in the table.
local CurrentRarity = "None"
for RarityName, RarityPercent in pairs(ChanceTable) do
if RNG < RarityPercent then
CurrentRarity = RarityName
end
end
Every time the number is lower than the rarity the "CurrentRarity" variable changes to suit the one you passed.
After this, you can make another loop to reward the player:
for RarityName, RarityReward in pairs(RewardTable) do
if CurrentRarity == RarityName then
CurrencyValue.Value += RarityReward
end
end
It is also recommended to use a RemoteEvent if you're doing this on a LocalScript, since it won't save if you're doing this from there
1 comment
100% is full, your math doesn't make any sense. Just do it to 100%, to 1.