This is a rarity system I made a bit ago, you can use it if you want to also compare it to your original rarity system and you’ll probably find what went wrong, since a lot of time its just math and conditions, but from a glance it does seem like you got close
Modulescript
function RaritySystem.TotalWeight(Rarities)
local TotalWeight = 0
for i, item in ipairs(Rarities) do
TotalWeight = TotalWeight + item.weight
end
return TotalWeight
end
function RaritySystem.SelectRarity(Rarities)
local RandomValue
local SelectedRarity
local TotalWeight = RaritySystem.TotalWeight(Rarities)
repeat
RandomValue = RandomSeed:NextNumber(0, 1)
for _, Rarity in ipairs(Rarities) do
if RandomValue <= Rarity.weight / TotalWeight then
SelectedRarity = Rarity
break
else
RandomValue = RandomValue - Rarity.weight / TotalWeight
end
end
task.wait()
until SelectedRarity ~= nil
return SelectedRarity
end
function RaritySystem.CalculateAndDisplayChances(Rarities)
local TotalWeight = RaritySystem.TotalWeight(Rarities)
for _, Rarity in ipairs(Rarities) do
local chance = (Rarity.weight / TotalWeight) * 100
warn(Rarity.name .. ": " .. tostring(string.format("%.4f", chance)) .. "% chance")
end
end
Serverscript
Use case
local RarityWeights = {name = "Common", weight = 70},
{name = "Uncommon", weight = 25},
{name = "Rare", weight = 4},
{name = "Epic", weight = 2},
{name = "Legendary", weight = 0.01},
local SelectedRarity = RarityList.SelectRarity(RarityWeights) -- Gives rarity