Timer Failling

Hi, i’m a beginner in GLUA and i want to create script to save played time of players, the timer is falling and the drawed text don’t change value if you can help me plz

there is the code and the screeshots

SERVER


 
function Initialize()

	if ( !file.IsDir ( "play_time", "DATA" ) ) then

			file.CreateDir( "play_time", "DATA" )

	end

end

hook.Add( "Initialize", "InitializeData", Initialize )


function LoadData( ply )

	local userDataFile = "play_time/" .. ply:UniqueID() .. ".txt"

	if file.Exists( userDataFile, "DATA" ) then

		local playerData = util.JSONToTable( file.Read( userDataFile, "DATA" ) )
		playerData.joins = playerData.joins + 1
		playerData.time = playerData.time + math.Round(CurTime())
		file.Write( userDataFile, util.TableToJSON( playerData ) )

	else 

		
		data = {

				steamid = ply:SteamID64(), 
				joins = 1,
				name = ply:Name(),
				uniqueid = ply:UserID(),
				time = math.Round(CurTime())

				}

		file.Write( userDataFile, util.TableToJSON( data ) )

	end

	ply.timeConnected = CurTime()

end

hook.Add( "PlayerInitialSpawn", "LoadPlayerData", LoadData )

timer.Create( "CalculateTime", 1, 0, function()
		 
	for k, v in pairs( player.GetAll() ) do 
		v.playingTime = math.Round( v.timePlayer + getConnectedTime( v ) )
		v.SetNWInt( "time_played", v.playingTime)
	end

end )

function getConnectedTime( ply )
	return CurTime() - ply.timeConnected
end

function SavePlayer( ply )
	local userDataFile = "play_time/"..ply:UniqueID()..".txt"
	file.Write( userDataFile, CurTime() )
end

hook.Add( "PlayerDisconnected", "SavePlayerTime", SavePlayer ) 

CLIENT




function timeToStr( time )

	local tmp = time
	local s = tmp % 60
	tmp = math.floor( tmp / 60 )
	local m = tmp % 60
	tmp = math.floor( tmp / 60 )
	local h = tmp % 24
	tmp = math.floor( tmp / 24 )
	local d = tmp % 7
	tmp = math.floor( tmp / 7 )
	local w = tmp % 4
	tmp = math.floor( tmp / 4 )
	local mn = tmp % 12

	return string.format( "%i Month  %i Week  %02i Day  %02i Hour  %02i Minutes  %02i Secondes", mn, w, d, h, m, s )

end

function PaintHUD()

	draw.RoundedBox( 1, ScrW()/2 - 163, 10, 320, 40, Color( 0, 0, 0, 230 ) )
	draw.DrawText( timeToStr( LocalPlayer():GetNWInt( "time_played" ) ), "DermaDefault", ScrW()/2, 22, color_white, TEXT_ALIGN_CENTER )

end

hook.Add( "HUDPaint", "PaintHUD", PaintHUD )

SCREENSHOTS

One error I spotted (only a quick check)

v.SetNWInt( “time_played”, v.playingTime)

should be

v:SetNWInt( “time_played”, v.playingTime)

(little explanation: v.SetNWInt(self,“time_played”, v.playingTime) is equivalent to v:SetNWInt(“time_played”, v.playingTime) so your program was not executing well. )

How it did not crash? I have no clue

other thing:

check this http://wiki.garrysmod.com/page/Player/TimeConnected

as you are creating exactly this function in your code.

thx for your help, i forget it but the code is not working yet

Here is a fast and small code snippet that I have just made (and it is working as expected):

Server side :



function Initialize()

	if ( !file.IsDir ( "play_time", "DATA" ) ) then

			file.CreateDir( "play_time", "DATA" )

	end

end

hook.Add( "Initialize", "InitializeData", Initialize )

local meta = FindMetaTable("Player")

function meta:Time_LoadDataFile()
	local userDataFile = "play_time/" .. self:UniqueID() .. ".txt"

	if file.Exists( userDataFile, "DATA" ) then
		self.time_playerData = util.JSONToTable( file.Read( userDataFile, "DATA" ) )
		self.time_playerData.joins = self.time_playerData.joins + 1
	else


		self.time_playerData = {

				steamid = self:SteamID64(),
				joins = 1,
				name = self:Name(),
				uniqueid = self:UserID(),
				time = 0
				}
	end

  self:SetNWInt("TimePlayed",self.time_playerData.time )

	file.Write(userDataFile, util.TableToJSON(self.time_playerData))


end

function meta:SavePlayer()
	local userDataFile = "play_time/".. self:UniqueID() ..".txt"
  self.time_playerData.time = self:GetNWInt("TimePlayed")
	file.Write(userDataFile, util.TableToJSON(self.time_playerData))
end


hook.Add( "PlayerInitialSpawn", "LoadPlayerData", function(ply) ply:Time_LoadDataFile() end )


hook.Add( "PlayerDisconnected", "time_WhenPlayerDisconnect", function(ply)  ply:SavePlayer()end )

timer.Create( "CalculateTime", 1, 0, function()

	for k, v in pairs( player.GetAll() ) do
    if v then
  		local playingTime = v:GetNWInt("TimePlayed") + 1
  		v:SetNWInt( "TimePlayed", playingTime)
      -- v:SavePlayer() -- Because PlayerDisconnected does not work in single player
    end
	end

end )







I kept your idea but just modified the syntax to make it correct

Client Side:

Just do a




  function timeToStr( time )

	local tmp = time
	local s = tmp % 60
	tmp = math.floor( tmp / 60 )
	local m = tmp % 60
	tmp = math.floor( tmp / 60 )
	local h = tmp % 24
	tmp = math.floor( tmp / 24 )
	local d = tmp % 7
	tmp = math.floor( tmp / 7 )
	local w = tmp % 4
	tmp = math.floor( tmp / 4 )
	local mn = tmp % 12

	return string.format( "%i Month  %i Week  %02i Day  %02i Hour  %02i Minutes  %02i Secondes", mn, w, d, h, m, s )

end

function PaintHUD()

	draw.RoundedBox( 1, ScrW()/2 - 163, 10, 320, 40, Color( 0, 0, 0, 230 ) )
	draw.DrawText( timeToStr( LocalPlayer():GetNWInt( "TimePlayed" ) ), "DermaDefault", ScrW()/2, 22, color_white, TEXT_ALIGN_CENTER )

end

hook.Add( "HUDPaint", "PaintHUD", PaintHUD )



I hope every part is understandable!

The reason why your code generates the error is that you’re calling the getConnectedTime() function every second for every connected player, even those that haven’t spawned yet, and thus don’t have the ‘timeConnected’ property.

Thank you very much, the code is working (v:SavePlayer() is working without server xD)