• Time played script
    11 replies, posted
I need a nice script that counts the time the player has been on the server. Can anyone do it? Would be really nice.
ULX/ULib + UTime?
Here: [code] if ( SERVER ) then local function TSInitialSpawn( ply ) local sid = ply:SteamID() local time if ( file.Exists( "TimeSpent/" .. sid .. ".txt" ) ) then time = file.Read( "TimeSpent/" .. sid .. ".txt" ) if time == "" then time = 0 end else time = 0 end ply:SetNWFloat( "ts_spent", tonumber( time ) ) ply:SetNWFloat( "ts_time", ply:TimeConnected() ) end hook.Add( "PlayerInitialSpawn", "TSInitialSpawn", TSInitialSpawn ) local function TSThink() for k, v in pairs( player.GetAll() ) do local ts = v:GetNWFloat( "ts_spent" ) v:SetNWFloat( "ts_time", v:TimeConnected() + ts ) end end hook.Add( "Think", "TSThink", TSThink ) local function TSDisconnected( ply ) local connecttime = ply.ConnectTime local sid = ply:SteamID() file.Write( "TimeSpent/" .. sid .. ".txt", ply:GetNWFloat( "ts_time" ) ) end hook.Add( "PlayerDisconnected", "TSDisconnected", TSDisconnected ) end local function FormatTime( seconds, format ) local i = math.floor( seconds ) local h = i / 3600 local m = ( i / 60 ) - ( math.floor( i / 3600 ) * 3600 ) local s = seconds - ( math.floor( i / 60 ) * 60 ) return string.format( format, h, m, s ) end if ( CLIENT ) then surface.CreateFont( "ChatFont", 15, 400, false, true, "TimeSpent" ) local function TSPaint() local ply = LocalPlayer() local time = ply:GetNWFloat( "ts_time" ) local format = FormatTime( time, "%02i:%02i:%02i" ) surface.SetFont( "TimeSpent" ) surface.SetTextColor( 255, 255, 255, 255 ) surface.SetTextPos( 0, 0 ) surface.DrawText( "Time spent:" ) surface.SetTextPos( ScrH() * 0.12, 0 ) surface.DrawText( format ) end hook.Add( "HUDPaint", "TSPaint", TSPaint ) end [/code]
[QUOTE=Foss;16077290]Here: [code] // code[/code][/QUOTE] Will not write the file, because : is an invalid character when it comes to saving files. Instead, use this. [code]if ( SERVER ) then local function TSInitialSpawn( ply ) local sid = ply:SteamID():gsub(":" , "_") local time if ( file.Exists( "TimeSpent/" .. sid .. ".txt" ) ) then time = file.Read( "TimeSpent/" .. sid .. ".txt" ) if time == "" then time = 0 end else time = 0 end ply:SetNWFloat( "ts_spent", tonumber( time ) ) ply:SetNWFloat( "ts_time", ply:TimeConnected() ) end hook.Add( "PlayerInitialSpawn", "TSInitialSpawn", TSInitialSpawn ) local function TSThink() for k, v in pairs( player.GetAll() ) do local ts = v:GetNWFloat( "ts_spent" ) v:SetNWFloat( "ts_time", v:TimeConnected() + ts ) end end hook.Add( "Think", "TSThink", TSThink ) local function TSDisconnected( ply ) local connecttime = ply.ConnectTime local sid = ply:SteamID():gsub(":" , "_") file.Write( "TimeSpent/" .. sid .. ".txt", ply:GetNWFloat( "ts_time" ) ) end hook.Add( "PlayerDisconnected", "TSDisconnected", TSDisconnected ) end local function FormatTime( seconds, format ) local i = math.floor( seconds ) local h = i / 3600 local m = ( i / 60 ) - ( math.floor( i / 3600 ) * 3600 ) local s = seconds - ( math.floor( i / 60 ) * 60 ) return string.format( format, h, m, s ) end if ( CLIENT ) then surface.CreateFont( "ChatFont", 15, 400, false, true, "TimeSpent" ) local function TSPaint() local ply = LocalPlayer() local time = ply:GetNWFloat( "ts_time" ) local format = FormatTime( time, "%02i:%02i:%02i" ) surface.SetFont( "TimeSpent" ) surface.SetTextColor( 255, 255, 255, 255 ) surface.SetTextPos( 0, 0 ) surface.DrawText( "Time spent:" ) surface.SetTextPos( ScrH() * 0.12, 0 ) surface.DrawText( format ) end hook.Add( "HUDPaint", "TSPaint", TSPaint ) end[/code]
Thanks for fixing it.
What i did is i made a timeplayed.lua and included it in my init.lua file. Is this how to make this work? (sorry, im new to lua) EDIT: Also, How do i view the time played? Is there a command, or does it make a text file?
Either init.lua or shared.lua, not cl_init because that's client-side only.
It goes into garrysmod/lua/autorun, save it as timespent.lua or something.
Can someone tell me how to make this work on my server? As stated above, I made a file called timeplayed.lua and included it into my int.lua Is that correct because it wont work.
You put it into lua/autorun and you don't need to include it in your init.lua.
hmm, okay thank you.
[video=youtube;7SrIDTgWjgk]http://www.youtube.com/watch?v=7SrIDTgWjgk[/video]
Sorry, you need to Log In to post a reply to this thread.