Choosing a rarity takes a while or cant be looped

I’m trying to create a rarity game inspired by infinite rarities and such games. However i’m running into an issue with looping the rarity table after the player reaches a really high luck value and can’t get anything on the rarity table anymore.

The script i used before i tried looping it was this:

local module = {}

local bNum = require(game.ReplicatedStorage.Bnum)

function module.chooseIndex(rarityTable, luck)
	local newRarityArray = {}
	
	local totalWeight = bNum.floattobnum(0)
	for i, rarity in ipairs(rarityTable) do
		local weight = 1/5
		weight = bNum.pow(bNum.floattobnum(weight), bNum.sub(bNum.floattobnum(i), bNum.floattobnum(1)))
		local newWeight = bNum.mul(weight, bNum.strtobnum(luck))
		if bNum.me(newWeight, bNum.floattobnum(1)) then
			continue
		end

		totalWeight = bNum.add(totalWeight, newWeight)
		table.insert(newRarityArray, {[rarity[1]] = {newWeight}})
		--newRarityArray[rarity] = {newWeight}
	end
	local rnd = bNum.rand(bNum.floattobnum(0), totalWeight)
	
	local selectedRarity = "Common"
	local accumulatedWeight = bNum.floattobnum(0)
	local found = false
	for index, rarity in pairs(newRarityArray) do
		for a, b in pairs(rarity) do
			accumulatedWeight = bNum.add(accumulatedWeight, b[1])
			if bNum.leeq(rnd, accumulatedWeight) then
				print(accumulatedWeight)
				selectedRarity = a
				found = true
				break
			end
		end
		
		if found == true then
			break
		end
		
	end
	return selectedRarity
end

return module

it uses BigNum and newwil’s tutorial (i changed the formula for the weights a bit)

This is the script I tried to make for the rarity table to loop so you would get a rarity like “Epic+200”.

local function getRarities(rarityTable, luck)
	local newRarityArray = {}

	local totalWeight = bNum.floattobnum(0)
	for i, rarity in ipairs(rarityTable) do
		local weight = 1/5
		weight = bNum.pow(bNum.floattobnum(weight), bNum.sub(bNum.floattobnum(i), bNum.floattobnum(1)))
		local newWeight = bNum.mul(weight, bNum.strtobnum(luck))
		if bNum.me(newWeight, bNum.floattobnum(1)) then
			continue
		end

		totalWeight = bNum.add(totalWeight, newWeight)
		table.insert(newRarityArray, {[rarity[1]] = {newWeight}})
		--newRarityArray[rarity] = {newWeight}
	end
	return totalWeight, newRarityArray
end

local function chooseRarity(newRarityArray, rnd)
	local selectedRarity = "Common"
	local accumulatedWeight = bNum.floattobnum(0)
	local found = false
	for index, rarity in pairs(newRarityArray) do
		for a, b in pairs(rarity) do
			accumulatedWeight = bNum.add(accumulatedWeight, b[1])
			if bNum.leeq(rnd, accumulatedWeight) then
				print(accumulatedWeight)
				selectedRarity = a
				found = true
				break
			end
		end

		if found == true then
			return true, selectedRarity
		else
			return false, nil
		end

	end
end

function module.chooseIndex(rarityTable, luck)
	local totalWeight, newRarityArray = getRarities(rarityTable, luck)

	local rnd = bNum.rand(bNum.floattobnum(0), totalWeight)
	local found, selectedRarity = chooseRarity(newRarityArray, rnd)
	local loops = 0
	while not found do
		wait()
		print(luck)
		luck = bNum.div(bNum.strtobnum(luck), bNum.floattobnum(92))
		totalWeight, newRarityArray = getRarities(rarityTable, bNum.bnumtostr(luck))
		rnd = bNum.rand(bNum.floattobnum(0), totalWeight)
		found, selectedRarity = chooseRarity(newRarityArray, rnd)
		loops += 1
		luck = bNum.bnumtostr(luck)
	end
	print("got rarity: "..selectedRarity.." with loops: "..loops)
	return selectedRarity
end

It devides your luck by 92 because thats how many rarities there are (idk that’s how i thought it would work). However, this is extremely slow and if I remove the wait() the game becomes extremely laggy.

If you have any further questions I’d be happy to answer them.

1 Like

took a whole different approach and managed to achieve “infinite” rarities

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