Best way to count time played with PData?

-snip- Resolved.


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)


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


function CurSpawnTime(ply)
ply.JoinTime = CurTime();
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);
ply:SetPData(“TimePlayed”, Time);
hook.Add(“PlayerDisconnect”, “CurLeaveTime”, CurLeaveTime);

**[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