How to track server uptime

Hey developers!

So this is probably some really easy thing, but how would I be able to efficiently track the server uptime?
Any help would be appreciated!

Have a nice day!

2 Likes

When the server starts just keep a loop that updates the uptime value

such as

while wait(60) do
  serverUptime.Value += 1
end

Very simple

1 Like

If I wanted to create a h:m:s stopwatch, would this be an efficient way too, or would it lag too much?

1 Like

what do you mean lag?
A loop every second won’t lag your game!

while wait(1) do
  if minutes.Value < 60 then
    second.Value += 1
  else
    if minutes.Value < 60 then
      minutes += 1
    else
      hours.Value += 1
    end
  end
end
6 Likes

Well, I already have a lot of while wait(1) do loops in my game, but I guess this will be an ok way. Thank you so much for the help!

You can use “time converter” It will make from Seconds minutes hours and even days

Is it like an official feature or module? Can’t seem to find it on the DevHub.

3 Likes

No,d on’t use wait() it will be a disaster because wait() is inaccurate. I’ve already made stopwatches using wait before and the results are off by a lot.

If you don’t want accuracy, feel free to go ahead and use it.
Otherwise, I give you a better option here.

Use function time() and it will tell you exactly in seconds how much time your server has been running.

Ok. So I changed my script from this:

--Variables--
local serveruptimevalue = game:GetService("ServerStorage"):WaitForChild("ServerUptime")
local serveruptimeinseconds = 0

--Script--
while wait(1) do
    serveruptimeinseconds += 1
    
    local seconds = serveruptimeinseconds % 60
    local minutes = math.floor(serveruptimeinseconds % (60 * 60) / 60)
    local hours = math.floor(serveruptimeinseconds % (60 * 60 * 24) / (60 * 60))
    local days = math.floor(serveruptimeinseconds % (60 * 60 * 24 * 30) / (60 * 60 * 24))
    
    serveruptimevalue.Value = days .. ":" .. hours .. ":" .. minutes .. ":" .. seconds
end

to this:

--Variables--
local serveruptimevalue = game:GetService("ServerStorage"):WaitForChild("ServerUptime")

--Script--

while wait(1) do
    local seconds = time() % 60
    local minutes = math.floor(time() % (60 * 60) / 60)
    local hours = math.floor(time() % (60 * 60 * 24) / (60 * 60))
    local days = math.floor(time() % (60 * 60 * 24 * 30) / (60 * 60 * 24))
    
    serveruptimevalue.Value = days .. ":" .. hours .. ":" .. minutes .. ":" .. seconds
end

Is that correct?

(Edit: The last version gives me non-integers and almost crashes my Roblox Studio. I will still be using my old method.)

2 Likes

Yep, I think that looks better.