My system for earning cash every two minutes even while not playing the game isn't working

Godot Version

Godot 4.1

Question

I want to make it so that every two minutes you earn $3 I want this to be the case whether your playing the game or not the ammount of money you earn this way caps at 21600 (after 24hours passes.) and you will be able to claim the money after you press a button “ClaimCash”.

My approach was to save the last time you played in your user data and subtract it from the current time and then devide it by 120. This way you can see how many ‘2 minutes’ have passed. I called the function calculate_cash_earned()

Ok. Let me get to the problem. The first time I started the game every thing worked fine and the money to claim was going up by $3 every 2 minutes. I closed the game and waited a while to see if it worked while I wasn’t playing and I came back and that seemed to be working also. So I claimed my cash and the button reset to $0 and closed the game and reopened it in the span of a few seconds so not enough time for more cash to accumulate and to my suprise the Button didn’t show $0 to claim but instead the same ammount as before I closed the game. This was weird because I did claim it and it showed in the ammount of cash I had.

Here is my code, I suspect it has something to do with the _process(_delta) function:

extends CharacterBody2D

@onready var ClaimCash = $ClaimCash

var config = ConfigFile.new()
var lastplayed
var cashearned = 0

func _ready():
    if config.load("user://user_data.cfg") != OK:
        config.set_value("user", "user_lastplayed", Time.get_unix_time_from_system())
        config.save("user://user_data.cfg")
        lastplayed = config.get_value("user", "user_lastplayed")
    else:
        if not config.has_section_key("user", "user_lastplayed"):
            config.set_value("user", "user_lastplayed", Time.get_unix_time_from_system())
            config.save("user://user_data.cfg")
            lastplayed = config.get_value("user", "user_lastplayed")
        lastplayed = config.get_value("user", "user_lastplayed")
        cashearned = calculate_cash_earned()
        ClaimCash.text = str(cashearned)
        config.set_value("user", "user_lastplayed", Time.get_unix_time_from_system())
        config.save("user://user_data.cfg")

func calculate_cash_earned():
    var current_time = Time.get_unix_time_from_system()
    var elapsed_time = current_time - lastplayed
    var increments = int(elapsed_time / 120)
    cashearned = increments * 3
    return min(cashearned, 21600)

func _process(_delta):
    var current_time = Time.get_unix_time_from_system()
    if current_time - lastplayed >= 120:
        cashearned = calculate_cash_earned()
        ClaimCash.text = str(cashearned)
        lastplayed = current_time

func _on_claim_cash_pressed():
    var current_cash = config.get_value("user", "user_cash")
    var new_cash = current_cash + cashearned
    config.set_value("user", "user_cash", new_cash)
    config.save("user://user_data.cfg")
    cashearned = 0
    ClaimCash.text = str(cashearned)

I tried instead of resetting lastplayed every two minutes in the _process(_delta) function, I made a new variable with the same value as last played. It looked like this:

func _process(_delta):
    var current_time = Time.get_unix_time_from_system()'
        var lastearned = lastplayed
    if current_time - lastearned >= 120:
        cashearned = calculate_cash_earned()
        ClaimCash.text = str(cashearned)
        lastearned = current_time

This didn’t work + it added another problem. When I clicked on the button to claim the cash didnt reset to 0 instead it just stayed the same I didn’t even have to reopen the game this time.

You’re not saving the latest value of last_played to “user_lastplayed” in the config file.

You should update it on app exit or every time last_played is changed.

Ok I did that but now it’s doing the same thing but giving me a negative number instead. Maybe I did something wrong? This is an android game so I used NOTIFICATION_APPLICATION_PAUSED

func _notification(what):
	if what == NOTIFICATION_APPLICATION_PAUSED:
		config.set_value("user", "user_lastplayed", Time.get_unix_time_from_system())
		config.save("user://user_data.cfg")
		get_tree().quit()