• Best way to count time played with PData?
    14 replies, posted
-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
[QUOTE=Hyperion;30123587][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][/QUOTE] local k, v?? They will already be local to the timer..
[QUOTE=King Flawless;30123534]SysTime()[/QUOTE] Thanks for telling me about that function.
[QUOTE=c-unitV2;30123767]local k, v?? They will already be local to the timer..[/QUOTE] 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]
[b][url=http://wiki.garrysmod.com/?title=Player.TimeConnected]Player.TimeConnected [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] instead of relying on timers that can be inaccurate.
[QUOTE=c-unitV2;30142766]SysTime() returns the current system time[/QUOTE] Thanks for the info. [QUOTE=Commander11;30142942]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][/QUOTE] 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 [b]not[/b] a good idea at all..
[QUOTE=raBBish;30143116][b][url=http://wiki.garrysmod.com/?title=Player.TimeConnected]Player.TimeConnected [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] instead of relying on timers that can be inaccurate.[/QUOTE] Or you guys could use this, so much better
Sorry, you need to Log In to post a reply to this thread.