Jump to content

Server Uptime Counter


megaman54

Recommended Posts

Posted

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" ) 

Posted

Both, command and real counter are OK. But i dont really understand your code :|

EDIT: Lol now i get it! Thanks for the fast reply :D

I thought this would be more complicated than this but it wasn't :shock:

Nice! Thanks again :D!

Posted

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) 

Posted

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 ?

Posted

Your script is the same as mine, ignore that

You'll have to calc the amount of days, hours, minutes and so on out the seconds counter yourself, shouldnt be that hard when you think about it.

Posted

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.

Posted

When i first started thinking about this, i thought i should use getTickCount() but now, i know it can also done with a timer.

As eAi said, timer is unaccurate in this kind of use, then i will try to use getTickCount() as a replacement. Thanks. :D

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...