Jump to content

Server Uptime Counter


megaman54

Recommended Posts

setTimer to count the uptime in a variable, viewable in-game depends on your needs. A command or a real counter on the screen? A command is easily done with outputChatBox.

If a real counter is wanted, send the variable to the client using triggerClientEvent, and use another setTimer to update it there. Display it using dxDrawText in the onClientRender event.

local uptime = 0 
setTimer(function() uptime = uptime + 1 end, 1000, 0) 
  
-- command 
addCommandHandler 
outputChatBox 
  
-- on screen 
triggerServerEvent 
addEvent 
addEventHandler 
setTimer 
dxDrawText ( in "onClientRender" ) 

Link to comment

Ah well, it wasn't really a script. I gave you the functions you needed ( like you asked )

This could be the uptime command:

local uptime = 0 
setTimer(function() uptime = uptime + 1 end, 1000, 0) 
  
addCommandHandler('uptime', function(player) 
    outputChatBox ( "Current server uptime: " .. tostring (uptime) .. " seconds", player) 
end) 

Link to comment

Well, i already made a simple command but yours looks only a bit different.

My code:

local uptime = 0 
setTimer(function() uptime = uptime + 1 end, 1000, 0) 
   
function showUptime(player) 
    outputChatBox("Server uptime: " ..uptime.. " seconds", player, 0, 255, 0) 
end 
addCommandHandler("uptime", showUptime) 

EDIT: How to convert the seconds into days, months, years... etc ?

Link to comment

Using a timer like that is really bad for two reasons:

- setTimer doesn't guarantee that it'll fire after exactly 1000ms. It says that it'll fire after 1000ms, but it could be 1030ms or so, depending how fast your server is running. As a consequence, you're going to overestimate your uptime fairly significantly, adding roughly 12 minutes per day if your server runs at 60fps.

- It's also very wasteful triggering a function every second, and completely unnecessary.

As MCvarial says, you should use getTickCount(). Record the value of it when you start the server then just subtract that from the current value when you want to show it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...