Chance\Rarity system based on ply luck

I can’t understand how to make luck for better drop. How to add luck to better drop?


-- Variables
local balloonRarities = {
	{name = "Common", chance = 0.5},
	{name = "Uncommon", chance = 0.03},
	{name = "Rare", chance = 0.015},
        {name = "Epic", chance = 0.0015},
	{name = "Legendary", chance = 0.0005},
	
	--{name = "Common", chance = 1000},
	--{name = "Uncommon", chance = 250},
	--{name = "Rare", chance = 62.5},
	--{name = "Epic", chance = 15.625},
	--{name = "Legendary", chance = 3.90625},
	--{name = "Mystical", chance = 0.9765625},
	--{name = "Exotic", chance = 0.244140625},
}
local click = script.Parent
local dropperBonus = 0.1
-- local luck = 10

while true do
	local rarityRoll = math.random() -- Roll for balloon rarity
	local rarityThreshold = 0
	for i, rarity in ipairs(balloonRarities) do
		local testLuck = luck + rarity.chance
		rarityThreshold = rarityThreshold + rarity.chance
		if rarityRoll <= rarityThreshold then
			print(rarity.name)
				break
			end
		end
	wait(0.001)
end

One way to do “luck” is to re-roll and pick the better of the two rolls, Risk of Rain 2 does this for their luck. But this algorithm should be to a low “luck” number, even 3 rolls is pretty high.

local rarityRoll = math.random()
-- for each luck re-roll
for i = 0, luck do
    -- replace rarityRoll with the new roll, if higher.
    rarityRoll = math.max(math.random(), rarityRoll)
end
1 Like

Then I can’t really understand how I can properly set up the chances of dropping out of the table

Hi, I had a go at amending the code, rather than setting a threshold, it may be easier to simply compare the rarityRoll to the rarity.chance then reduce the rarityRoll by that amount. If the for loop does not meet any conditions i.e. does not reach the break point, you must have met the condition for the highest rarity level. The dropper bonus can then be added into the loop to increase the likelihood that your rarityRoll is higher (or the chance threshold is lower as below).

while true do
	local rarityRoll = math.random() -- Roll for balloon rarity
	for i, rarity in ipairs(balloonRarities) do
		if rarityRoll <= rarity.chance - dropperBonus then   --dropperBonus added here
			print(rarity.name)
			break
        else
            rarityRoll = rarityRoll - rarity.chance
		end
    print(balloonRarities[5].name)  --Amend integer to highest level or default.
	wait(0.001)
end
1 Like