Rebirth Price Calculation Lag

Currently how we calculate the price of a Rebirth is very inefficient and can cause massive performance decreases due to players being able to rebirth a large amount of times. The cause of this issue is our use of the while loop to perform our calculations.

The amount of iterations our while loop will perform is based on the amountOfRebirths number argument provided to the funcion. If this number is relatively low, then you don’t have anything to worry about. In hindsight this seems like a very obvious thing, but I somehow didn’t take into consideration that players may eventually be able to afford millions of rebirths.

I must admit that my level of knowledge in math is quite low, so I turned to my buddy ChatGPT to find an optimized solution, which would avoid using any loops at all. You can replace the CalculatePrice function located inside of the ReplicatedStorage/Configs/Rebirths module script with the following:

ReplicatedStorage/Configs/Rebirths.lua
function RebirthsConfig.CalculatePrice(currentRebirth: number, amountOfRebirths: number?)
	amountOfRebirths = amountOfRebirths or 1
	local price = RebirthsConfig.BasePrice + ((currentRebirth + amountOfRebirths - 1) * RebirthsConfig.IncreasePerRebirth)
	if amountOfRebirths > 1 then
		local rebirths = amountOfRebirths - 1
		price = ((2 * RebirthsConfig.BasePrice) + (rebirths * RebirthsConfig.IncreasePerRebirth)) * (amountOfRebirths / 2)
		price = price + (currentRebirth * ((amountOfRebirths * RebirthsConfig.IncreasePerRebirth) - RebirthsConfig.IncreasePerRebirth))
	end
	return price
end
Contents