Calculation issue creates lag/crashes (script timeout error)

  1. I want to calculate the requirement needed to prestige and reincarnate. Currently I am using a loop to do that when calculating multi-prestige/reincarnation

  2. It works just fine until the numbers get really big. Then the lag starts and when a player wants to prestige 1 million times, things stop working and I get the error message Script timeout: exhausted allowed execution time

  3. I have tried varies formulas to find out how to calculate the requirement without the need for a loop, but have been unsuccessful.

The original Prestige formula when a player wants to prestige just one time is:
requirement = 1000000*(currentPrestige.Value+1)

The original Reincarnation forumla when a player wants to reincarnate just one time is:
requirement = 15*(currentReincarnation.Value+1)

Here is the loop I use when a player wants to prestige multiple times at once

function module.MultiRebirthReq(plr, rebirth, num)
	local plrStats = plr:WaitForChild("PlayerStats")
	local reincarnation = plrStats.Reincarnation
	local calc = plrStats.Prestige.Value+1
	local reincarnationCalc = plrStats.Reincarnation.Value+1
	local requirement = 0

	if rebirth == "Prestige" then
		for count = 1, num do
			requirement = requirement+(1000000*(calc))
			calc = calc+1

	elseif rebirth == "Reincarnation" then
		for count = 1, num do
			requirement = requirement+(15*(reincarnationCalc))
			reincarnationCalc = reincarnationCalc+1


	return requirement

Maybe someone here has a better understanding of math and can guide me to a more efficient formula that does not need a loop or perhaps there is a way to avoid the lag/crash/time out issue in some other way

Trying to run anything more than about 10,000 times without a wait() involved is going to crash studio. Now I don’t know why you would even let people calculate prestige a million times. There are ways to calculate it without the loop, however the numbers you want to reach are beyond the normal capabilities of the Roblox engine.

The game started with basic numbers and players could prestige like 10 times at once. As the game progressed, more was required.

I tried the wait() but it only ceased the error, not the breaking of the game. I know there is a way to calculate this with a formula without a loop and have tried various ways, but couldn’t figure it out.

Are you wanting to achieve some form of exponential growth as a requirement for the next level or prestige, in this instance?

Yes, the requirement grows with each prestige/reincarnation. I figured it out though!

It is the sum of an arithmetic sequence.

Here is what I did:

	if rebirth == "Prestige" then
	 n = currentPrestige.Value+num
	 d = 1000000
	a = 1000000
	totalSum =  (n/2)*(2*a+(n-1)*d)
	difference = (currentPrestige.Value/2)*(2*a+(currentPrestige.Value-1)*d)

	requirement = totalSum - difference
elseif rebirth == "Reincarnation" then
	 n = currentReincarnation.Value+num
	 d = 15
	a = 15
	totalSum =  (n/2)*(2*a+(n-1)*d)
	difference = (currentReincarnation.Value/2)*(2*a+(currentReincarnation.Value-1)*d)

	requirement = totalSum - difference