Help Logging a players join time and leave time

so what i want is when player join log their join time then on leave compare the current time with their join time and then submit their steam id and time to an sql table



hook.Add("PlayerAuthed", "JoinTime", function( ply )
    ply.joinTime = CurTime()
end)

hook.Add("PlayerDisconnected", "SaveShit", function( ply )
     local sessionTime = CurTime() - ply.joinTime
     -- submit shit.
end)


Thnx Anon can always count on facepunch now just need to send it to a sql table i can do dat well thnx

[editline]15th September 2014[/editline]

BUT again lol i suck at sql… but i will see what i can do thanks.

[editline]15th September 2014[/editline]

BUT IF ANYONE knows how to do the sql stuff please help :slight_smile:

Untested! So some of it might not work.



-- Create Table
local function CreateTimeTable()
	if !sql.TableExists( "table_name" ) then
		sql.Query( "CREATE TABLE table_name ( SteamID varchar(255), Online int )" )
	end
end
hook.Add( "InitPostEntity", "CreateTimeTable", CreateTimeTable )

-- Save Playtime
local function SaveTimePlayer( ply )
	
	local sessionTime = CurTime() - ply.joinTime
	local SelectPlayer = sql.Query( "SELECT SteamID FROM table_name WHERE SteamID = " .. ply:SteamID() )
	
	if SelectPlayer then 	-- Not sure if this is right /:
		
		local SelectTime = sql.QueryValue( "SELECT time FROM table_name WHERE SteamID = " .. ply:SteamID() )
		
		local NewSessionTime = SelectTime['Time'] + sessionTime	-- Probably wrong too :/ Can't remember how it returns the value..
		
		sql.Query( "UPDATE table_name SET Time = " .. NewSessionTime .. " WHERE SteamID = " .. ply:SteamID() )
		
	else
	
		sql.Query( "INSERT INTO table_name ( SteamID, Time ) VALUES ( " .. ply:SteamID() .. ", " .. sessionTime ..  ")" )
	
	end
	
end
hook.Add( "PlayerDisconnected", "SaveTimePlayer", SaveTimePlayer )


[editline]15th September 2014[/editline]

It wont let me edit the post for some reason :confused: But yeah i made a error on line 4: Change Online to Time…