How can i display the time the server has been on

So im making a game, and just for fun im making a label that u can see the exact time the server is on

How would i make it so it display the time the server is on in a clock time, so like lets the server is on for 4 hours 30 minutes and 12 second it will display 04:30:12

Could anyone help me with that?

Grab the os.time() first then when a server is started and you can reference current os.time() to the initial and format it using os.date(). Say:

local startTime = os.time()--server just started

while task.wait(1) do
	print("server has been live for " .. os.date("!%X", os.time() - startTime))
end

You could also use os.difftime() instead of os.time() - startTime, but as we’re confident time is moving forward it shouldn’t be an issue for a counter like this.

Read more about this on the os documentation

1 Like

There exists two Roblox exclusive globals for this.

while task.wait(1) do
	print(time(), elapsedTime())
end

https://developer.roblox.com/en-us/api-reference/lua-docs/Roblox-Globals

They may be deprecated (although the documentation doesn’t state as such).

2 Likes