Waiting for variables to update from query before returning.

First of all, yes I am using mysqloo, I don’t really understand the new tmysql module, secondly excuse the way I turn a table into a usable number, it’s the best way I could come up with. (Suggestions welcomed)

Now on to my issue, I am coding a money system involving mysql, and I have a little issue regarding my function for getting the players cash:

[LUA]function GetCash(ply)
getcash = db:query(“SELECT cash FROM player_data WHERE(sid=’”…ply:SteamID()…"’)")

function getcash:onSuccess(data)
	local str = table.ToString(data)
	local str2 = string.Replace(str, "{{cash=", "")
	local str3 = string.Replace(str2, ",},}", "")
	cash = tonumber(str3)
return cash


However, this obviously returns the value cash, but whenever it is updated, it always is behind. So if the cash was 100 in the DB, and I used function GetCash, it would return 100. If I then changed it to 200, then run the function, it would return 100 still as it hasn’t had the time to update before being returned.

So of course I am wondering how I can fix this so it always returns the value of cash after it has been updated.


Here’s a basic example of a currency system…

But, it doesn’t do saving and loading. But, you should load the money when the player joins and set the var in a callback function. Don’t halt the server while the query executes unless you do it on GM:Shutdown ( where you’d save the data ) otherwise players will notice the half-second - second lag while it queries and that’ll be each and every time GetCash is called. Instead, get it once… set it… network it… share the AddMoney calls so that the server and client stays in sync ( but make sure the costs where money is removed is in a shared table so the client can try altering it but the client has final say ). Never trust the client to tell the server how much something costs.