How to make luck system of rarity system

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

Please help me :sob:

Thank you

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

well now i keep getting legendary with luck 2

also the values for the chances come out as 100 and i am dividing them to 0.1 (/100)

What are the uncommon, common, rare, epic, legendary values?

If you want it to be like this then I can’t help you, sorry

Common: 40
Epic: 9
Legendary: 1
Rare: 20
Uncommon: 30

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
1 Like