Where to Place SQL Money and Stat system?

I followed a basic tutorial here =“http://http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexb23a.html”] and =“http://www.facepunch.com/showthread.php?t=777892”]http://www.facepunch.com/showthread.php?t=777892 on SQL for a stat and money system and now would like to know how to incorporate it into a basic rp gamemode. Here is the code below:


function sql_value_stats ( Player )
        unique_id = sql.QueryValue("SELECT unique_id FROM player_info WHERE unique_id = '"..steamID.."'")
	money = sql.QueryValue("SELECT money FROM player_info WHERE unique_id = '"..steamID.."'")
	Player:SetNWString("unique_id", unique_id)
	Player:SetNWInt("money", money)
end

function sql_value_skills ( Player )
        unique_id = sql.QueryValue("SELECT unique_id FROM player_skills WHERE unique_id = '"..steamID.."'")
        speech = sql.QueryValue("SELECT speech FROM player_skills WHERE unique_id = '"..steamID.."'")
        sneak = sql.QueryValue("SELECT sneak FROM player_skills WHERE unique_id = '"..steamID.."'")
        farm = sql.QueryValue("SELECT farm FROM player_skills WHERE unique_id = '"..steamID.."'")
        Player:SetNWString("unique_id", unique_id)
        Player:SetNWInt("speech", speech)
        Player:SetNWInt("sneak", sneak)
        Player:SetNWInt("farm", farm)
end

function saveStat ( Player )
        money = Player:GetNWInt("money")
        unique_id = Player:GetNWString ("SteamID")
        speech = Player:GetNWInt("speech")
        sneak = Player:GetNWInt("sneak")
        farm = Player:GetNWInt("farm")
        sql.Query("UPDATE player_skills SET speech = "..speech..", sneak = "..sneak..", farm = "..farm.." WHERE unique_id = '"..unique_id.."'")
        sql.Query("UPDATE player_info SET money = "..money.." WHERE unique_id = '"..unique_id.."'")
        Player:ChatPrint("Stats updated !") // Added a nice little message
end
function table_exist()
        if sql.TableExists("player_infp") && sql.TableExists("player_skills") then
                Msg("Both tables already exist !")
        else
                if (!sql.TableExists("player_info")) then
                        query = "CREATE TABLE player_info (unique_id varchar(255), money int )"
                        result = sql.Query(query)
                        if (sql.TableExists("player_info")) then
                                Msg("Success ! table 1 created 
")
                        else
                                Msg("Something went wrong with the player_info query ! 
")
                                Msg( sql.LastError( result ) .. "
" )
                            end
                end
                if (!sql.TableExists("player_skills")) then
                        query = "CREATE TABLE player_skills (unique_id varchar(255), speech int, sneak int, farm int )"
                        result = sql.Query(query)
                        if (sql.TableExists("player_skills")) then
                                Msg("Succes ! table 2 created 
")
                        else
                                Msg("Something went wrong with the player_skills query ! 
")
                                Msg( sql.LastError( result ) .. "
")
                        end
                end
        
        end
end
 
 
function PlayerInitialSpawn( Player )
 
	timer.Create("Steam_id_delay", 1, 1, function()
		SteamID = Player:SteamID()
		Player:SetNWString("SteamID", SteamID)
		timer.Create("SaveStat", 10, 0, function() saveStat( Player ) end)
		player_exists( Player ) 
	end)
 
 end 

function player_exists ( Player )
	
    steamID = ply:GetNWString("SteamID")

    result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
    if (result) then
                    sql_value_stats(Player)
                    sql_value_skills(Player)
    else
            new_player( steamID, Player)
    end
end

function new_player( SteamID, Player )
		
        steamID = SteamID
		sql.Query( "INSERT INTO player_info (`unique_id`, `money`)VALUES ('"..steamID.."', '100')" )
		result = sql.Query( "SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'" )
		if (result) then
 
			sql.Query( "INSERT INTO player_skills (`unique_id`, `speech`, `sneak`, `farm`)VALUES ('"..steamID.."', '1', '1', '1')" )
			result = sql.Query( "SELECT unique_id, speech, sneak, farm FROM player_skills WHERE unique_id = '"..steamID.."'" )
			if (result) then
				Msg("Player account created !
")
				sql_value_stats( Player )
				sql_value_skills( Player )
			else
				Msg("Something went wrong with creating a players skills !
")
			end
 
		else
			Msg("Something went wrong with creating a players info !
")
		end
end

hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
hook.Add( "Initialize", "Initialize", Initialize )

hoping this answers anyone else’s question to, please help

So, you have the setup finished. Now what you are going to have to do is create functions that will allow you to manipulate the money values.(Adding money, subtracting money, etc.) To do this, lets take a look at what you have down.

We have our ‘local’, and by local I mean what we have grabbed from the SQL during the initialization and player initial spawn, that store your information for UID, money, speech, farm, and sneak. In order to manipulate them we can create functions to edit the networked variables( the ‘local’ copy ) and then call the save function you already declared, sql_value_skills( ply ).

Here is a quick example on how to do this with the money:




--A function to add money.

--_R.Player is the players meta table. We will add the function sql_addMoney(amount) to it so we can call it like this, ply:sql_addMoney( 15 )
function _R.Player:sql_addMoney( amount )
   
   --Get the current money of the player
   local money = self:GetNWInt("money");

   --Set the NW varb to the old money plus the amount we wanted to add.
   self:SetNWInt("money", money + amount);

   --Call your save function that you defined!
   sql_value_skills ( self );
   
end



The rest is up to you!