How can I make players keep credits upon leaving the server


	

    require("mysql")
    AddCSLuaFile("autorun/client/creditsmenu.lua")
    local ITEM
     
    function RegisterEmeraldsItem(tb)
            ITEM[string.upper(tb.Name)] = tb
            print(Format("Emeralds Credit System -> Registered item %s",tb.Name))
    end
     
    local function LoadEmeraldCreditModules()
            ITEM = {}
            local files = file.FindInLua("EmeraldCredits/sv_modules/*.lua")
            for k, v in pairs(files) do
                    include("EmeraldCredits/sv_modules/"..v)
            end
    end
    LoadD3CreditModules()
     
    concommand.Add("EmeraldCredits_Reload",function(p,c,a)
            if p and p:IsValid() then
                    if p:IsSuperAdmin() then
                            LoadEmeraldCreditModules()
                            for k,v in pairs(player.GetAll()) do
                                    v:ChatPrint("Emerald Credits was reloaded...")
                            end
                            print("Emerald Credits was reloaded...")
                    end
            else
                    LoadEmeraldCreditModules()
                    for k,v in pairs(player.GetAll()) do
                            v:ChatPrint("Emerald Credits was reloaded...")
                    end
                    print("Emerald Credits was reloaded...")
            end
    end)
     
    concommand.Add("EmeraldCredits",function(p,c,a)
            if a[1] then
                    if string.upper(a[1]) == "BUY" then
                            if a[2] then
                                    local i = string.upper(a[2])
                                    if ITEM* then
                                            local db, e = mysql.connect("localhost","donations","donations","donations")
                                            if db == 0 then
                                                    print(e)
                                            else
                                                    local tb, succ, e = mysql.query(db,Format("SELECT * FROM `Emeraldpoints` WHERE SteamID=%q",p:SteamID()))
                                                    if succ then
                                                            if tb then
                                                                    local bal = tonumber(tb[1][3])
                                                                    if bal >= ITEM*.Cost then
                                                                            if (ITEM*.Eligible(p)) then
                                                                                    tb, succ, e = mysql.query(db,Format("UPDATE `Emeraldpoints` SET Points=Points-%s WHERE SteamID=%q",ITEM*.Cost,p:SteamID()))
                                                                                    if !succ then print(e) end
                                                                                    p:ChatPrint(Format("You bought %s for %s credits!",ITEM*.Name,ITEM*.Cost))
                                                                                    p:ChatPrint(Format("Your account balance is now %s credits.",bal-ITEM*.Cost))
                                                                                    ITEM*.Give(p)
                                                                                    for k,v in pairs(player.GetAll()) do
                                                                                            if v != p then
                                                                                                    v:ChatPrint(Format("%s just bought %s with their Emerald Credits!",p:Name(),ITEM*.Name))
                                                                                            end
                                                                                    end
                                                                            else
                                                                                    p:ChatPrint(Format("You are not currently eligible to purchase %s!", item*.Name))
                                                                            end
                                                                    else
                                                                            p:ChatPrint(Format("You don't have enough credits to purchase %s!",ITEM*.Name))
                                                                    end
                                                            end
                                                    else
                                                            print(e)
                                                    end
                                            end
                                            mysql.disconnect(db)   
                                    end
                            end
                    end
            end
    end)
     
    hook.Add("PlayerInitialSpawn","EmeraldCredits",function(p)
            local db, e = mysql.connect("localhost","donations","donations","donations")
            if db == 0 then
                    print(e)
            else
                    local tb, succ, e = mysql.query(db,Format("SELECT COUNT(*) FROM `Emeraldpoints` WHERE SteamID=%q",p:SteamID()))
                    if succ then
                            if tb then
                                    if tonumber(tb[1][1]) < 1 then
                                            tb, succ, e = mysql.query(db,Format("INSERT INTO `Emeraldpoints` (`SteamID`,`Points`) VALUES(%q,'0')",p:SteamID()))
                                            if !succ then print(e) end
                                    else
                                            tb, succ, e = mysql.query(db,Format("SELECT * FROM `Emeraldpoints` WHERE SteamID=%q",p:SteamID()))
                                            if succ then
                                                    timer.Simple(5,function()
                                                            if p and p:IsValid() then
                                                                    p:ChatPrint(Format("Welcome %s! You have %s credits on your account to spend.",p:Name(),tb[1][3]))
                                                                    p:ChatPrint("Say /emerald to access the credit store now!")
                                                            end
                                                    end)
                                            else
                                                    print(e)
                                            end
                                    end
                            end
                    else
                            print(e)
                    end
            end
            mysql.disconnect(db)
    end)
     
    concommand.Add("GetEmeraldCreditInfo",function(p,c,a)
            if !p or !p:IsValid() then return end
            umsg.Start("EmeraldCreditItems",p)
                    umsg.Short(table.Count(ITEM))
                    local k,v
                    for k,v in pairs(ITEM) do
                            umsg.String(v.Name)
                            umsg.String(tostring(v.Cost))
                            umsg.String(v.Description)
                    end
            umsg.End()
     
            umsg.Start("EmeraldCreditBalance",p)
                    local db, e = mysql.connect("localhost","donations","donations","donations")
                    if db == 0 then
                            print(e)
                    else
                            local tb, succ, e = mysql.query(db,Format("SELECT COUNT(*) FROM `Emeraldpoints` WHERE SteamID=%q",p:SteamID()))
                            if succ then
                                    if tb then
                                            tb, succ, e = mysql.query(db,Format("SELECT * FROM `Emeraldpoints` WHERE SteamID=%q",p:SteamID()))
                                            if succ then
                                                    //print("--> "..tb[1][3])
                                                    umsg.String(tb[1][3])
                                            else
                                                    print(e)
                                            end
                                    end
                            else
                                    print(e)
                            end
                    end
                    mysql.disconnect(db)
            umsg.End()
    end)



You seem to already know how to work with mySQL, so hooking onto the PlayerDisconnected or EntityRemoved should be cake… unless that’s not your code.