• SQL Help
    2 replies, posted
[lua] function sql_value_stats ( ply ) 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.."'") ply:SetNWString("unique_id", unique_id) ply:SetNWInt("money", money) end function saveStat ( ply ) money = ply:GetNWInt("money") unique_id = ply:GetNWString ("SteamID") sql.Query("UPDATE player_info SET money = "..money.." WHERE unique_id = '"..unique_id.."'") ply:ChatPrint("Stats updated !") end function tables_exist() if (sql.TableExists("player_info")) 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("Succes ! table 1 created \n") else Msg("Somthing went wrong with the player_info query ! \n") Msg( sql.LastError( result ) .. "\n" ) end end end end function new_player( SteamID, ply ) 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`, `fish`, `farm`)VALUES ('"..steamID.."', '1', '1', '1')" ) result = sql.Query( "SELECT unique_id, speech, fish, farm FROM player_skills WHERE unique_id = '"..steamID.."'" ) if (result) then Msg("Player account created !\n") sql_value_stats( ply ) else Msg("Something went wrong with creating a players skills !\n") end else Msg("Something went wrong with creating a players info !\n") end end function player_exists( ply ) steamID = ply:GetNWString("SteamID") result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'") if (result) then sql_value_stats( ply ) else new_player( steamID, ply ) end end function Initialize() tables_exist() end function PlayerInitialSpawn( ply ) timer.Create("Steam_id_delay", 1, 1, function() SteamID = ply:SteamID() ply:SetNWString("SteamID", SteamID) timer.Create("SaveStat", 10, 0, function() saveStat( ply ) end) player_exists( ply ) end) end hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn ) hook.Add( "Initialize", "Initialize", Initialize ) [/lua] The problem is only the last person who joined is getting saved.
Line 82 [lua]timer.Create("SaveStat", more args)[/lua] Timers are supposed to have unique names. So when you create that timer, it overwrites other timers with that name. So do something like [lua]timer.Create("SaveStat"..ply:SteamID(), more args)[/lua]
[media]http://www.youtube.com/watch?v=_LDKIwEVrhc[/media] Thank you so much! It works perfectly.
Sorry, you need to Log In to post a reply to this thread.