I want to make it so that the play can do an upgrade for all of his money. I would normally do this with a simple formula, but the thing is, the way that price is calculated, it requires level to passed to it, because the price is exponential. But, to calculate the level, I need to now the price, so I know how many upgrades you can do for the money you have. So the only solution i found is to use the while loop, but the thing is, if player has million coins it will mean that the loop would have to do million repetitions, so I need to include task.wait, which makes it take minutes, but I need to be no more than a second long. Maybe there is a formula I can use, or a do a loop better, I would appreciate any help. Here’s that part of the code
local UpgradeData = Upgrades[Upgrade] -- info about the upgrade
local PlayerUpgrade = PlayerData.Upgrades[Upgrade] -- player's upgrade info
local TotalLevel = PlayerUpgrade[1] -- current level
local TotalCost = 0
while PlayerData.things > 0 do
local CostMultiplier = UpgradeData.CostMultiplier + (TotalLevel - 1) * (TotalLevel - UpgradeData.CostMultiplier)
local Cost = UpgradeData.Cost * CostMultiplier
Cost = math.floor(Cost + 0.5)
PlayerData.things -= Cost
TotalCost += Cost
TotalLevel += 1
task.wait()
end
--do the buy
So if I’m understanding the problem you face correctly (apologies if I don’t!) The player can purchase an upgrade that will do something to the player? And you want to check how many upgrades the player is eligible for (ex. if an upgrade cost $1 and the player has 1,000 coins, they can buy it 1,000 times.)
You are correctly that this would cost a long time to do it in the loop you have.
For this, I would highly recommend searching: Arithmetic Sequences. There is a specific formula apart of it, that can find the value of the term (ex. term 1 could be 3 and term 100 could be 20,000. *these are not real numbers/an equation) This could be useful in this situation, or you’d have to do the loop that would take time. If you have problems with understanding/finding information on Arithmetic sequences, just reply to this and I’ll help
Hello! Sorry for replying after 1d, I wasn’t home so I couldn’t answer. So you were talking about Arithmetic Sequences (or Arithmetic Progression) which is what I’m using to calculate cost of the upgrade. And technically I can reverse this to get the amount of levels from the money, but I don’t really understand how can I do this.
To find, lets say the ‘20th’ term (20th upgrade) the formula you would need is:
Tn = a+(n-1)d
a = the first term (the cost of the first upgrade)
n = the nth term (the term you want to find out, in this case, the 20th term)
d = the common difference. (How much the sequence changes each term. Ex. T2 - T1 (The cost of the second term, minus the cost of the first term)
You can use the answer of this formula to find the cost of that exact term, without having to write out every single term!
Thank you for the reply! What you wrote makes sense, but you see, you’re formula (almost just like mine) requires an upgrade value (like you’re saying 20 as an example here), but what I need, is to somehow flip that formula so it can give me the amount of upgrades (upgrade value) for all the money I have, and I don’t really know how
Ah! I understand now, so sorry it took me this long!
Disclaimer, this was a headache to work out. I utilised chatGPT to assist me with this.
This is the Sum for an Arithmetic Sequence, but rearrganed to find ’ n ’ which is the number of upgrades applicable for the amount you have.
In this formula;
the total amount of upgrades you can afford - n
the initial cost of the first upgrade - a
the increased cost of each upgrade (for my example it’d be 20) - d