Playtime Tracking

I’m looking for a simple playtime tracker in seconds. I can do conversions myself, I’m not good with SQL though. Serverside please.

I don’t have any money to offer, only my <3

Thanks in advanced.

  • Snip -

Something like this?
[lua]hook.Add(“Initialize”, “InitializeDatabase”, function()
if( !sql.TableExists(“player_playtime”) )then
local result = sql.Query(“CREATE TABLE player_playtime ( unique_id varchar(255), playtime int )”);

    if !result then
        ErrorNoHalt(sql.LastError);
    end;
end;

end);

hook.Add(“PlayerInitialSpawn”, “SaveTimeOnPlayerInitialSpawn”, function(ply)
ply.timeJoined = CurTime();
ply.totalTimePlayed = 0;

local result = sql.Query("SELECT playtime FROM player_playtime WHERE unique_id = '".. ply:UniqueID() .."'");

if result then
    ply.totalTimePlayed = tonumber(result);
end;

end);

hook.Add(“PlayerDisconnected”, “SaveTimeOnPlayerDisconnected”, function(ply)
local sessiontime = CurTime() - ply.timeJoined;
local newtotaltime = ply.totalTimePlayed + sessiontime;

if( ply.totalTimePlayed == 0 )then
    local result = sql.Query("INSERT INTO player_playtime (`unique_id`, `playtime`)VALUES ('".. ply:UniqueID() .."', '".. newtotaltime .."')");

    if !result then
        ErrorNoHalt(sql.LastError);
    end;
else
    local result = sql.Query("UPDATE player_playtime SET playtime = ".. newtotaltime .." WHERE unique_id = '".. ply:UniqueID() .."'");

    if !result then
        ErrorNoHalt(sql.LastError);
    end;
end;

end);[/lua]

I’ve never worked with the sql library before and this is untested. If it doesn’t work, sorry D:

Thanks, but I’m getting “tried to preform arithmetic; timeJoined is nil”

[lua]hook.Add(“PlayerDisconnected”, “SaveTimeOnPlayerDisconnected”, function(ply)
local sessiontime = CurTime() - ply.timeJoined;[/lua]

line 83 is the error, or the second line I posted.

Fixed. It was late and I made a stupid mistake using the wrong hook.

<3 thank you!