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