Hello im trying to make a rarity system with pseudo infinite rarities using eternitynum 2, but i dont wanna make each rarity using the classic table/weight method, so i need to calculate each rarity on the fly, what would be the best way to do it, because im always exceeding the time with the loops
this is the current script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local dm = require(ServerStorage:WaitForChild("DataModule"))
local EN = require(ReplicatedStorage:WaitForChild("EternityNum"))
local Raritier = {}
local Config = {
BaseChance = 4, -- 1βinβ4 at tier=1
DecayFactor = 1.75, -- each fail: denom *= 1.75
MaxTier = 1e10,
Debug = false,
}
local ZERO = EN.fromNumber(0)
local ONE = EN.fromNumber(1)
-- reuse a single RNG for performance
local RNG = Random.new()
local function debug(label, val)
if Config.Debug then
print(("[DEBUG] %s = %s"):format(label, EN.short(val)))
end
end
local function rollSimpleRarityEN(startTier)
local tier = EN.convert(startTier or 1)
local rollCap = EN.fromNumber(Config.BaseChance)
local decayFac = EN.fromNumber(Config.DecayFactor)
local tierMax = EN.fromNumber(Config.MaxTier)
debug("StartTier", tier)
debug("BaseDenom", rollCap)
debug("DecayFactor", decayFac)
while true do
-- try rolling from current tier up to MaxTier
while EN.le(tier, tierMax) do
local p = EN.recip(rollCap)
local r = EN.nextNumber(ZERO, ONE, RNG)
if EN.le(r, p) then
-- success at this tier
local suffix = EN.toSuffix(tier)
return suffix, EN.toSuffix(rollCap)
end
-- failure: bump tier & denom
tier = EN.add(tier, ONE)
rollCap = EN.mul(rollCap, decayFac)
end
tier = ONE
rollCap = EN.fromNumber(Config.BaseChance)
debug("Reset Tier to", tier, "and BaseDenom to", rollCap)
-- and loop again until success
end
end
function Raritier.new(StartTier, playerId)
if not dm.WaitForProfile(playerId, 5) then
warn("[Raritier] Profile not loaded β defaulting tier roll")
return rollSimpleRarityEN(StartTier)
end
local tier, prob = rollSimpleRarityEN(StartTier)
warn("[RESULT]: ",tier)
warn("[PROBABILITY]: ",prob)
return tier, prob
end
return Raritier