How will I go about making a chance system that supports Luck Boosts?

What do I do after that? Do I use the thing you showed me?

You should probably recalculate the chances depending on their multiplier before you sort them. Then when you get the random number, you should multiply the maximum by the max multiplier as well.

Im gonna go eat lunch, please list a lot of script examples on what I can do…

function calculateChanceMultiplier(chance, maxMultiplier) -- Calculates the multiplier for each rarity
	return maxMultiplier/maxChance * chance
end

local function selectRarity(player) -- Selects one of the rarities provided
	local luck = player.Boosts.Luck.Value
	-- Get the rarities, multiply them, and sort them
	local rarityTable = totalStats:GetChildren() -- table with all the chance values
	for _, chance in pairs(rarityTable) do -- Calculate the new chances by multiplying with luck boost
		chance.Value *= calculateChanceMultiplier(chance.Value, luck)--multiply with multiplier
	end
	table.sort(rarityTable, function(a, b)
		return a.Value > b.Value
	end
	-- Select one of them
	local rand = math.random(rarityTable[1].Value, rarityTable[#rarityTable].Value)
	for _, chance in pairs(rarityTable) do
		if chance.Value >= rand then
			return chance -- Picked this one
		end
	end
end

From here, you’ll want to have the client invoke the server to Select a rarity and give it to the player, as well as tell the client which one it received so that it can do the appropriate effects locally.

These functions might not work entirely with your script and you might need to fix them up a bit.

I changed things up and it says that argument #2 for random is empty.

local maxChance = 25000

function calculateChanceMultiplier(chance, maxMultiplier) -- Calculates the multiplier for each rarity
	return maxMultiplier/maxChance * chance
end

local function selectRarity(player) -- Selects one of the rarities provided
	local luck = player.Boosts.Luck.Value
	-- Get the rarities, multiply them, and sort them
	local rarityTable = totalStats -- table with all the chance values
	local rarities = {}
	
	for _, chance in pairs(rarityTable) do -- Calculate the new chances by multiplying with luck boost
		table.insert(rarities, {chance.Name, chance.Value * calculateChanceMultiplier(chance.Value, luck)})
	end
	table.sort(rarities, function(a, b)
		return a[2] > b[2]
	end)
	-- Select one of them
	local rand = math.random(rarities[1][2], rarities[#totalStats][2])
	for _, chance in pairs(rarities) do
		if chance[2] >= rand then
			return StatsFolder[chance[1]] -- Picked this one
		end
	end
end

try to make #totalStats #rarities instead.

local rand = math.random(rarities[1][2], rarities[#rarities][2])

Did that too, still output the same result.

Did you convert the totalStats to a table or is it still a folder? If its a folder you need to :GetChildren().

I tried putting the maxChance in #2 argument, BUT it always gave me the rarest rarity.

It’s using :GetChildren() so thats not the issue.

By the way, I printed the table, and I checked what the rarest rarity is, and its the same value.

I forgot that we’d have to switch the values for the random around since the table is sorted higehst to lowest and also, we’ll need to implement the reversed multiplier from earlier here because of that:

function calculateChanceMultiplier(chance, maxMultiplier)
	return -maxMultiplier/maxChance * chance + maxMultiplier
end
local rand = math.random(rarities[#rarities][2], rarities[1][2])

I tested and it runs properly for me, let me know if it works for you.

Now it’s giving me the second rarest rarity!!! Just so you know, my brain is being fried :wink:

Okay, I totally messed it up… I tested it out myself and I realized how wrong the code was I wrote… No reason to sort it, it uses a weight based system anyway. I tweaked it a bit, let me know if this works for you:

function calculateChanceMultiplier(chance, maxMultiplier)
	return maxMultiplier / maxChance * chance + 1
end

local function selectRarity(player) -- Selects one of the rarities provided
	local luck = player.Boosts.Luck.Value
	local weight = 0
	-- Get the rarities, multiply them, and sort them
	local rarities = {}

	for _, chance in pairs(totalStats) do -- Calculate the new chances by multiplying with luck boost
		local multipliedChance = chance.Value * calculateChanceMultiplier(chance.Value, luck)

		weight += multipliedChance
		table.insert(rarities, {chance.Name, multipliedChance})
	end

	-- Select one of them
	local rand = math.random(1, weight)
	
	weight = 0
	for _, chance in pairs(rarities) do
		
		weight += math.abs(chance[2] - maxChance)
		if weight >= rand then
			print(chance[1])
			return StatsFolder[chance[1]]
		end
	end
end

EDIT: Removing the whole luck stuff should make it work perfeclty fine… but I got to work around it a bit and changed it a bit to decrease the chances of everything, just not decrease the chance of the rare ones as much as the common ones. This basically increases your chance of getting rarer ones when luck boost is active, but its kinda weird and hacky. When luck is at 1, its the default, anything above is basically multiplied… 0 probably does some weird stuff.

If it still doesn’t work, try removing the luck portion, otherwise the values you have set up might be a lot more different than what I imagined. Sorry I couldn’t find a proper solution, but I hope this helps!

1 Like

It’s still mainly giving me the rare stuff.
Do you wanna test it for yourself?
Link: The Game

You there? Or are you not? Because you left the game…

Remove the +1 for more accurate chances!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.