I have this game that involves collecting money over time. They can use the money to spawn units to protect their base, upgrade wallet, etc. My problem is that the amount of money keeps going over the maximum they are supposed to have. Also, for some reason the money increases irregularly, almost as if there are 2 timers going on at once.
Here is the code that controls the incrementation:
local function increaseMoney() if moneyCount == moneyWallet then else moneyCount = moneyCount + 10 moneyText.text = tostring(moneyCount) .. " / " .. tostring(moneyWallet) end end local moneyCounterTimer = timer.performWithDelay(increasePerTime, increaseMoney, 0) local function upgradeUpdate() increasePerTime = increasePerTime - 10 moneyLVL = moneyLVL + 1 moneyCount = moneyCount - costToUpgrade costToUpgrade = costToUpgrade + 100 moneyWallet = moneyWallet + 335 end local function upgradeMoney(event) if event.phase == "began" then if moneyCount == costToUpgrade or moneyCount \> costToUpgrade then if moneyLVL \< 8 then upgradeUpdate() upgradeText.text = "Upgrade: \nLevel " .. tostring(moneyLVL) costUpgradeText.text = "$" .. tostring(costToUpgrade) moneyText.text = tostring(moneyCount) .. " / " .. tostring(moneyWallet) timer.cancel(moneyCounterTimer) local moneyCounterTimer = timer.performWithDelay(increasePerTime, increaseMoney, 0) end if moneyLVL == 8 then upgradeText.text = "Upgrade: \nMAX " upgradeText.fontSize = 15 costUpgradeText.text = "" end else print("Insufficient Funds") end end end upgradeCapEfi:addEventListener("touch", upgradeMoney)
Any help would be great! Plus, I added a video for your convenience.