JSON help/tips

Hey guys so I am starting from scratch using a skeleton gamemode. However, I want the teams to save automatically upon disconnection. For example:

team.SetUp( 1, “Guest”, Color( 125, 125, 125, 255 ) )

When a user leaves he/she would be team guess.

I am not asking this to be done for me, but I won’t complain if you make me an example. All pointers/answers/possible solutions/ideas are going to mean the universe to me. Keep in mind I have nothing and don’t even know where to begin with this saving process, or how to write a file for this. I’ve seen it done before though so I have a scarce amount of familiarity.

EDIT: Also just for clarification when a user rejoins their team should be replenished. I REALLY have no idea how to do that… >.<

hook.Add("PlayerDisconnect", "SavePlayerData", function(ply)
	if not file.Exists("/playerdata","DATA") then
	local data = {}
	data.team = ply:Team()
	data = util.TableToJSON(data, true)
	file.Write("/playerdata/"..ply:SteamID64()..".txt", data)

hook.Add("PlayerInitialSpawn", "LoadPlayerData", function(ply)
	if not file.Exists("/playerdata/"..ply:SteamID64()..".txt", "DATA") then return end
	local data = file.Read("/playerdata/"..ply:SteamID64()..".txt", "DATA")
	data = util.JSONToTable(data)

Le serverside code.

Wow thank you, however I just got a error.

[ERROR] gamemodes/skeleton/gamemode/init.lua:62: attempt to concatenate a nil value
  1. fn - gamemodes/skeleton/gamemode/init.lua:62
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:179

if not file.Exists("/playerdata/"..ply:SteamID64()..".txt", "DATA") then return end

is line 62


Also, the hook is “PlayerDisconnected” not “PlayerDisconnect”.

I’m sorry, but even when I set it up on my dedicated server it doesn’t appear to save for whatever reason. Here is my init.lua: Sorry in advanced if I am doing anything wrong/stupid.

EDIT: I join and my team is unset, I have to run the created console command to create a team. o.0

Sorry, I coded that all without watching the wiki xD
@OP on line 46, try removing the “/” infront of the directory name

Here is what whitestar said to do, but did

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
// Serverside only stuff goes here
   Name: gamemode:PlayerLoadout( )
   Desc: Give the player the default spawning weapons/ammo
 function GM:PlayerLoadout(ply) --"The weapons/items that the player spawns with" function
        ply:StripWeapons() -- This command strips all weapons from the player.
        if ply:Team() == 1 then --If the player is on team "Guest"...
                ply:Give("weapon_physcannon") -- ...then give them the Gravity Gun.
        elseif ply:Team() == 2 then -- Otherwise, if the player is on team "Another Guest"...
                ply:Give("weapon_physgun") -- ...then give them the Phys Gun.
        end -- This ends the if/elseif.
end -- This ends the function.
function GM:ShowSpare2( ply ) -- This hook is called everytime F1 is pressed.
    umsg.Start( "MyMenu", ply ) -- Sending a message to the client.
end --Ends function
function team_1( ply )
    ply:SetTeam( 1 )
function team_2( ply )
    ply:SetTeam( 2 )
hook.Add("PlayerDisconnected", "SavePlayerData", function(ply)
        if not file.Exists("skeletongamemodedata","DATA") then
        local data = {}
        data.team = ply:Team()
        data = util.TableToJSON(data, true)
        file.Write("skeletongamemodedata/"..ply:SteamID64()..".txt", data)
hook.Add("PlayerInitialSpawn", "LoadPlayerData", function(ply)
        if not file.Exists("skeletongamemodedata/"..ply:SteamID64()..".txt", "DATA") then return end
        local data = file.Read("skeletongamemodedata/"..ply:SteamID64()..".txt", "DATA")
        data = util.JSONToTable(data)
concommand.Add( "team_1", team_1 )
concommand.Add( "team_2", team_2 )