Hello I have a rarity system and i want to put luck system but it never gives me legendary even though luck should give me more chance to get legendary
Script
luck = luck or 1
print("Luck Recieved:",luck)
local totalchance = uncommon+common+rare+epic+legendery
print("Total Chance:",totalchance)
local Rarity = {
['Uncommon'] = uncommon/totalchance*luck,
["Common"] = common/totalchance*luck,
["Rare"] = rare/totalchance*luck,
["Epic"] = epic/totalchance*luck,
["Legendary"] = legendery/totalchance*luck
}
local Weight = 0
for _, Chance in pairs(Rarity) do
Weight+=(Chance*10)
end
local RanNumber = math.random(1,Weight)
Weight = 0
for Rarty, Chances in pairs(Rarity) do
Weight+=(Chances*10)
if Weight>=RanNumber then
--print("Rarity:",Rarty..", Chance:",Chances)
return Rarty
end
end
You are multiplying the numbers by the same amount so it’s still the same chance for each. Here’s how I did this by converting them into 1 / chance format:
luck = luck or 1
print("Luck Recieved:",luck)
local totalchance = uncommon+common+rare+epic+legendery
print("Total Chance:",totalchance)
local Rarity = { -- turn into 1 / chance
['Uncommon'] = totalchance/uncommon, -- this is (1 in number) chance
["Common"] = totalchance/common,
["Rare"] = totalchance/rare,
["Epic"] = totalchance/epic,
["Legendary"] = totalchance/legendery
}
local Weight = 0
for i, Chance in pairs(Rarity) do
Rarity[i] = Chance / luck -- modify table
Weight += Rarity[i] -- add new value to weight
-- assume luck is 5, chance is 50, that means new chance is 1 in (50 / 5)
end
local RanNumber = Random.new():NextNumber(0, Weight) -- this supports decimals
Weight = 0
for Rarty, Chances in pairs(Rarity) do
Weight += Chances
if Chances > 1 then -- avoid too common rarities (> 100%)
if Weight >= RanNumber then
--print("Rarity:",Rarty..", Chance:",Chances)
return Rarty
end
end
end
I made some mistakes in the code by not converting it into 1 / number format, here’s the fixed one:
luck = luck or 1
print("Luck Recieved:",luck)
local totalchance = uncommon+common+rare+epic+legendery
print("Total Chance:",totalchance)
local Rarity = { -- turn into 1 / chance
['Uncommon'] = totalchance/uncommon, -- this is (1 in number) chance
["Common"] = totalchance/common,
["Rare"] = totalchance/rare,
["Epic"] = totalchance/epic,
["Legendary"] = totalchance/legendery
}
local Weight = 0
for i, Chance in pairs(Rarity) do
if Chance / luck >= 1 then
Rarity[i] = 1 / (Chance / luck) -- modify table
Weight += Rarity[i] -- add new value to weight
else Rarity[i] = nil -- avoid too common rarities (> 100%)
end
-- assume luck is 5, chance is 50, that means new chance is 1 in (50 / 5)
end
local RanNumber = Random.new():NextNumber(0, Weight) -- this supports decimals
Weight = 0
for Rarty, Chances in pairs(Rarity) do
Weight += Chances
if Weight >= RanNumber then
--print("Rarity:",Rarty..", Chance:",Chances)
return Rarty
end
end