Player Server Age

Hello, how could I time how long a player is in a server and put it on a GUI
image
Like under the Name display, there time I.E Hours: 1, Minutes: 1, Seconds: 1

You could start an Int/Number Value to count upwards when the player joins, something like this:

game:GetService("Players").PlayerAdded:Connect(function(plr)
   local TimeValue = Instance.new("IntValue")
   TimeValue.Name = "PlayerServerAge"
   TimeValue.Parent = plr
end)

coroutine.wrap(function()
   while true do
       if TimeValue then
         TimeValue.Value += 1
      end
   end
end)()

And then using a .Changed event you could update the TextLabel locally, while showing the format using this resource.

unlocal TimeValue

also thanks :smiley:

I instead advise using the unix timestamp.
Use this on the server:

game.Players.PlayerAdded:connect(function(plyr)
	local v = Instance.new('NumberValue')
	v.Name = 'JoinedAt'
	v.Value = os.time()
	v.Parent = plyr
end)

And use this to get the time spent:

local timeSpent = math.floor(workspace:GetServerTimeNow() - plyr.JoinedAt.Value)

Your method also has a memory leak and causes a crash. :sweat_smile:

1 Like

And I could use Converting secs to (h:min:sec) - #2 by Uglypoe for time conversion, correct?

Yes you can. Or u can use this function that I have

function SecondsToClock(a)
	local num = tonumber(a)
	local days = math.floor(num / 86400)
	if days ~= 0 then
		return days .. " day(s)"
	end
	if num <= 0 then
		return "00:00:00"
	end
	local h = string.format("%02.f", math.floor(num / 3600))
	local m = string.format("%02.f", math.floor(num / 60 - h * 60))
	return h .. ":" .. m .. ":" .. string.format("%02.f", math.floor(num - h * 3600 - m * 60))
end
1 Like

Nice, thanks for the solution!

This works, but im not 100% the best at code practice so is something like this good, to use in production.

	while true  do
		wait(1)
		local timeSpent = math.floor(workspace:GetServerTimeNow() - Player.JoinedAt.Value)
		local timeSpentFormat = SecondsToClock(timeSpent)
		SelfTime.Text = timeSpentFormat or 'NILL'	
	end