Incremental trying to upgrade hundreds of thousands of times breaking the script

Hello, I’m working on an incremental (Infinite Rarities: Ascended), and there are upgrades that you upgrade for points, problem is, I’ve gotten to a point you get a ton of points and when trying to upgrade it does the math for upgrading hundreds of thousands of times, getting to a point the time to execute the script is timed out and the script literally breaks.

How would I be able to calculate this in 1 go or in a way more efficient way that won’t cause problems now or in the future? (because as the level updates the price changes as well)

The current formula for the price is: Level^4

I’m not quite sure what it is you’re actually calculating making this nearly impossible to answer. Can you clarify why this is looping so many times? This might be easiest to answer if you supply the code that’s causing this looping which will help in making sure everyone is on the same page.

I’m calculating the cost to upgrade, here’s the loop

while true do
			local IncreasedLevelCap = 0
			
			if PlayerData.Upgrades[UpgradeInfo.PointType.."CapIncrease"] then
				IncreasedLevelCap = UpgradeData:GetBoost(UpgradeInfo.PointType.."CapIncrease", PlayerData.Upgrades[UpgradeInfo.PointType.."CapIncrease"])
			end
			
			if UpgradeInfo["CapIncreaseInmune"] then
				IncreasedLevelCap = 0
			end
			
			if PlayerData.Upgrades[Upgrade.Name] >= (UpgradeInfo.MaxLevel + IncreasedLevelCap) then break end
			if PlayerData[UpgradeInfo.PointType] < UpgradeData:GetPrice(Upgrade.Name, PlayerData.Upgrades[Upgrade.Name] + 1) then break end
			
			PlayerData[UpgradeInfo.PointType] = PlayerData[UpgradeInfo.PointType] - UpgradeData:GetPrice(Upgrade.Name, PlayerData.Upgrades[Upgrade.Name] + 1)
			PlayerData.Upgrades[Upgrade.Name] = PlayerData.Upgrades[Upgrade.Name] + 1
			UpgradeAmount = UpgradeAmount + 1
		end

You can see the math being done and how many times this probably is repeated, causing the problems.