SQL autosave is defaulting to the original value?

Forgive me if it’s a simple solution, Sunday was my first day with an attempt at Lua coding.

I followed the SQL tutorial on the wiki to make myself a basic SQL database that saves and loads.

I tried to test it by making a console command that increases how much “money” the player has… it stays until the autosave, which then reverts it back to the default of 100 “money”.

cl_init.lua:
[lua]include( ‘shared.lua’ )

function CheckMoney( ply )
money = ply:GetNWInt( “money” )
Msg( money … "
" )
end

function AddMoney( ply )
ply:SetNWInt( “money”, money + 5 )
money = ply:GetNWInt( “money” )
Msg( money … "
" )
Msg( "5 Money added
" )
end

function LoseMoney( ply )
ply:SetNWInt( “money”, money - 5 )
money = ply:GetNWInt( “money” )
Msg( money … "
" )
Msg( "5 Money removed
" )
end

concommand.Add( “CheckMoney”, CheckMoney )
concommand.Add( “AddMoney”, AddMoney )
concommand.Add( “LoseMoney”, LoseMoney )[/lua]

The ‘autosave’ function in sql_database.lua:
[lua]function saveStat ( ply )
money = ply:GetNWInt( “money” )
unique_id = ply:GetNWString ( “SteamID” )
speech = ply:GetNWInt( “speech” )
fish = ply:GetNWInt( “fish” )
farm = ply:GetNWInt( “farm” )
sql.Query( “UPDATE player_skills SET speech = “…speech…”, fish = “…fish…”, farm = “…farm…” WHERE unique_id = '”…unique_id…"’" )
sql.Query( “UPDATE player_info SET money = “…money…” WHERE unique_id = '”…unique_id…"’" )
ply:ChatPrint( “Autosaving…” )
end[/lua]

I’ve looked around the wiki and topics on the forum, but I can’t seem to find a similar problem.

If you need any additional information on the code, I can give it. Thanks for any help.

PS. Also, how do I determine where I should put functions? I’m not sure if I should put some functions on clientside/shared/serverside- I’m hoping I got lucky by making the SQL database a serverside script.

You want to make the SQL database a serverside script. Why would you want other players to determine their skill :wink:

Alright, then I’ve got the file placement for it right…

But there’s still the problem of it not saving.

Probably because setting networked integers doesn’t work on the client, so the money never changes and thus never gets saved as anything different than 100.

Alright, so how do I go about solving this?

I tried putting that client function into the serverside lua, but there was no reaction clientside or on the console of my dedicated server.

ply:SetNWInt( “money”, money + 5 )
money = ply:GetNWInt( “money” )

These two lines, in each command, are the wrong way round. When it runs the first line it tries to find the value of ‘money’ so it can add 5 to it. But money doesn’t exist yet, because it doesn’t get defined until the next line. The second line should start ‘local money’ as well, because you’re only using it as a temporary value, so making it local stops it clogging up the global environment and conflicting with other variables that have that name. In your save function, all tho fish, speech, farm, money etc lines should have local in front as well.

As for it not saving, I assume you’ve created the table with the proper fields and datatypes?

As for where functions go, use logic. The server is going to be the one with the big database of player stats, right? If each player stored his own stats he could very easily alter them outside your server, and come back as a super character. Same goes for altering cash level - surely you don’t want players to be able to alter their cash willy nilly? Because you’re running the script from the client, the setting networked variable functions don’t work, because that’s another thing clients shouldn’t have the power to do.