Help with Idle Death Gamble math

Me and my friend are tryna make the Idle Death Gamble domain expansion for Hakari, but we are really not sure how the math works. We should have everything we need from the color multis to the scenario multis (Tell us if we are missing something though) but we don’t know how to actually put those numbers together.

local Attempts = 100

local RaritySystem = {}

function RaritySystem.GetItem(Rarities)
	local RNG = Random.new(); -- You could use math.random, this is just what I chose.
	local Counter = 0;
	for i, v in pairs(Rarities) do
		Counter += Rarities[i][2]
	end
	local Chosen = RNG:NextNumber(0, Counter);
	for i, v in pairs(Rarities) do
		Counter -= Rarities[i][2]
		if Chosen > Counter then
			return Rarities[i][1]
		end
	end
end

local HakariRarities = {
	Color = {
		{1, 1/1},--Green multi
		{2, 1/3},--Red multi
		{3, 1/10},--Gold multi
	},
	Scenario = {
		{239, 1/1},--One star.
		{50, 1/5},--Two star. 50% chance boost.
		{1.25, 1/15},--Three star. 80% chance boost.
	},
	--Consecutive jackpot multi = 7.97
}

local EffectHistory:{number} = {}

local function GetConsecutiveMulti(Effect:number)
	table.insert(EffectHistory,Effect)
	for index, ExistingEffect in pairs(EffectHistory) do
		if ExistingEffect ~= Effect then
			EffectHistory = {}
			return 1
		end
	end
	if #EffectHistory >= 4 then
		EffectHistory = {}
		return math.huge
	else
		return #EffectHistory
	end
end

local Jackpot = false

local function IdleDeathGamble()
	local JackpotChance = RaritySystem.GetItem(HakariRarities.Scenario)
	if Jackpot == true then
		--local Calcul = 0.75 * RaritySystem.GetItem(HakariRarities.Scenario)
		JackpotChance = 0.25 * RaritySystem.GetItem(HakariRarities.Scenario)-- - Calcul
	end
	
	local MinChance = 1
	
	local EffectMulti1 = RaritySystem.GetItem(HakariRarities.Color)
	local EffectMulti2 = RaritySystem.GetItem(HakariRarities.Color)
	GetConsecutiveMulti(EffectMulti1)
	local ConsecutiveMulti = GetConsecutiveMulti(EffectMulti2)
	MinChance *= EffectMulti1
	MinChance *= EffectMulti2
	MinChance *= ConsecutiveMulti

	--local JackpotMulti = Jackpot and 7.97 or 1
	--MinChance = MinChance/JackpotMulti

	if math.random(1,JackpotChance) <= MinChance then
		Jackpot = true
		Attempts = 0
		print("🎰 Jackpot HIT!")
	else
		Jackpot = false
		print("💥 Jackpot FAILED!")
	end

	if true then
		print("Starting chance: 1/"..JackpotChance)
		print("Effect 1 multi: "..EffectMulti1)
		print("Effect 2 multi: "..EffectMulti2)
		print("Consecutive effect multi: "..ConsecutiveMulti)
		warn("Final chance: "..MinChance/JackpotChance * 100 .."% or "..MinChance.."/"..JackpotChance)
	end

	while Jackpot == true do
		wait(1)
		IdleDeathGamble()
	end

end

while Attempts > 0 do
	Attempts -= 1
	IdleDeathGamble()
	task.wait()
end

We have tried multiple ways to do the math, but everything doesn’t seem right. This is our last attempt so far and we are hoping someone else is more able to help us with this.

Arent casinos not allowed anymore on Roblox?

You tell me. Is something of this caliber not allowed on Roblox?

Pretty sure it’s only any gambling the player actually pays real money in any way to initiate that is banned.

add up the percentages

then let m = 1/(sum of percentages)

multiply every percentage by m, so now percentages sum to 1 and match the range of the random function

(now inside of GetItem function)
let r = a random number using RNG:NextNumber

iterate through the table, subtract the percentage from r

if r is less than or equal to 0, then select this element

I am not very good at math so… Most of this confused me even more. But this is what I tried… Then I completely lost you at the GetItem part.

local Attempts = 100

local RaritySystem = {}

function RaritySystem.GetItem(Rarities)
	local RNG = Random:NextNumber()

	-- Step 1: Sum up all probabilities
	local TotalProbability = 0
	for _, v in pairs(Rarities) do
		TotalProbability += v[2]
	end

	-- Step 2: Normalize probabilities
	local m = 1 / TotalProbability
	for _, v in pairs(Rarities) do
		v[2] *= m -- Scale probabilities so they sum to 1
	end

	-- Step 3: Random number selection
	local r = RNG:NextNumber() -- Random number between 0 and 1
	for _, v in pairs(Rarities) do
		r -= v[2] -- Subtract probability from random number
		if r <= 0 then
			return v[1] -- Select this item
		end
	end

	-- Fallback (should never reach here if the probabilities are correct)
	return Rarities[#Rarities][1]
end


local HakariRarities = {
	Color = {
		{1, 1/1},--Green multi
		{2, 1/3},--Red multi
		{3, 1/10},--Gold multi
	},
	Scenario = {
		{0, 1/1},--One star.
		{50, 1/5},--Two star. 50% chance boost.
		{80, 1/15},--Three star. 80% chance boost.
	},
}

local EffectHistory:{number} = {}

local function GetConsecutiveMulti(Effect:number)
	table.insert(EffectHistory,Effect)
	for index, ExistingEffect in pairs(EffectHistory) do
		if ExistingEffect ~= Effect then
			EffectHistory = {}
			return 1
		end
	end
	if #EffectHistory >= 4 then
		EffectHistory = {}
		return math.huge
	else
		return #EffectHistory
	end
end

local Jackpot = false

local function IdleDeathGamble()
	local JackpotChance = RaritySystem.GetItem(HakariRarities.Scenario)
	if Jackpot == true then
		--local Calcul = 0.75 * RaritySystem.GetItem(HakariRarities.Scenario)
		JackpotChance = 0.25 * RaritySystem.GetItem(HakariRarities.Scenario)-- - Calcul
	end

	local Percent = RaritySystem.GetItem(HakariRarities.Scenario) + (Jackpot == true and 70 or 0)

	local EffectMulti1 = RaritySystem.GetItem(HakariRarities.Color)
	local EffectMulti2 = RaritySystem.GetItem(HakariRarities.Color)
	GetConsecutiveMulti(EffectMulti1)
	local ConsecutiveMulti = GetConsecutiveMulti(EffectMulti2)
	
	local m = 1/Percent
	Percent *= m

	if idk then
		Jackpot = true
		Attempts = 0
		print("🎰 Jackpot HIT!")
	else
		Jackpot = false
		print("💥 Jackpot FAILED!")
	end

	if true then
		print("Starting chance: 1/"..JackpotChance)
		print("Effect 1 multi: "..EffectMulti1)
		print("Effect 2 multi: "..EffectMulti2)
		print("Consecutive effect multi: "..ConsecutiveMulti)
		--warn("Final chance: "..MinChance/JackpotChance * 100 .."% or "..MinChance.."/"..JackpotChance)
	end

	while Jackpot == true do
		wait(1)
		IdleDeathGamble()
	end

end

while Attempts > 0 do
	Attempts -= 1
	IdleDeathGamble()
	task.wait()
end

your getitem function should be working correctly