Best way to count time played with PData?

-snip- Resolved.

SysTime()

[lua]
timer.Create("_CountPlayerTime", 1, 0, function()
local k, v;

    for k, v in pairs(player.GetAll()) do
            local playerTime = v:GetPData("_TimePlayed");
            v:SetPData(playerTime + 1)
    end

end);
[/lua]

Calling it once a second seems a bit much

local k, v?? They will already be local to the timer…

Thanks for telling me about that function.

No they will be local to the loop not the timer

One more question, does SysTime continue from where it left off if a player reconnects or a map changes?

SysTime() returns the current system time

Hi?

[lua]
function CurSpawnTime(ply)
ply.JoinTime = CurTime();
end
hook.Add(“PlayerInitialSpawn”, “CurSpawnTime”, CurSpawnTime);

function CurLeaveTime(ply)
local Time = ply.JoinTime - CurTime();
if ply:GetPData(“TimePlayed”) then
ply:SetPData(“TimePlayed”, ply:GetPData(“TimePlayed”) + Time);
else
ply:SetPData(“TimePlayed”, Time);
end
end
hook.Add(“PlayerDisconnect”, “CurLeaveTime”, CurLeaveTime);
[/lua]

**[Player.TimeConnected

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.TimeConnected)** instead of relying on timers that can be inaccurate.

Thanks for the info.

Thanks for giving out code but I think I am going to stick with my current method with SysTime()

Your code wont when the server crashes.

You should hook a timer to make it save every 10 minutes etc.

Uhm. setting pdata every second is not a good idea at all…

Or you guys could use this, so much better