[Snippet] Show uptime in server list

I just wrote this quick script because I thought having uptime shown on the fivem server list is very useful.

Installation

There’s no download because I didn’t justify someone downloading a file for the tiny code. Here’s how to very easily implement it to your server, simply read the installation manual and you should be good to go!

  1. Add the following to your server configuration file:
    add_ace resource.uptime command.sets allow
    In this case the resource has been named uptime (you can change it)
  2. Add the following to a server sided file:
Citizen.CreateThread(function()
	local uptimeMinute, uptimeHour, uptime = 0, 0, ''

	while true do
		Citizen.Wait(1000 * 60) -- every minute
		uptimeMinute = uptimeMinute + 1

		if uptimeMinute == 60 then
			uptimeMinute = 0
			uptimeHour = uptimeHour + 1
		end

		uptime = string.format("%02dh %02dm", uptimeHour, uptimeMinute)
		SetConvarServerInfo('Uptime', uptime)


		TriggerClientEvent('uptime:tick', -1, uptime)
		TriggerEvent('uptime:tick', uptime)
	end
end)
26 Likes

Create a download its better.

8 Likes

Didn’t you read the entire thread? Motivate why it’s ‘better’ to have a download

1 Like

That way people don’t have to waste there time copying and creating files and shit.

6 Likes

A seperate resource for 30 lines of code?

@TroooperCorentin

I am pretty sure you meant: I am a lazy fuck and would like to just download and extract it. or I am too stupid to create the files myself.

22 Likes

Awesome Release :))
Thanks <3

Nice stuff man… Keep up the good work!

Hey Ice nice to see you active

Great release man! Keep it up :slight_smile:

It keep saying *sets command was denied in console on my server. I named it uptime and did
add_ace resource.uptime command.sets allow …

why your codes long and complicated
why are you use this function:

function FormatUptime(hour, minute)
	if hour <= 9 then
		hour = ('0%s'):format(hour)
	end

	if minute <= 9 then
		minute = ('0%s'):format(minute)
	end

	return ('%sh %sm'):format(hour, minute)
end

use this:

Citizen.CreateThread(function()
	local uptimeMinute = 0
	local uptimeHour = 0

	while true do
		Citizen.Wait(1000 * 60) -- each minute
		uptimeMinute = uptimeMinute + 1
	
		if uptimeMinute == 60 then
			uptimeMinute = 0
			uptimeHour = uptimeHour + 1
		end

		ExecuteCommand(string.format("sets Uptime \"%02dh %02dm\"", uptimeHour, uptimeMinute))
	end
end)

if you want this exact then use this:

Citizen.CreateThread(function()
	local starttick = GetGameTimer()
	while true do
		Citizen.Wait(15000) -- check all 15 seconds
		local tick = GetGameTimer()
		-- if you want days then use this:
		-- local uptimeDay = math.floor(tick-starttick)/86400000)
		-- if you use day then change the hour to this:
		-- local uptimeHour = math.floor(tick-starttick)/3600000) % 24
		local uptimeHour = math.floor((tick-starttick)/3600000)
		local uptimeMinute = math.floor((tick-starttick)/60000) % 60
		--if you want seconds then use this:
		--local uptimeSecond = math.floor((tick-starttick)/1000) % 60
		ExecuteCommand(string.format("sets Uptime \"%02dh %02dm\"", uptimeHour, uptimeMinute))
	end
end)

Because I’m not similar with the formatting, sorry for making you puke. I’ll improve the code thanks to you.

Do i add this file add_ace resource.uptime command.sets allow to my server.cfg and save it like just put it on top or anywhere sorry im new to this stuff

nice thanks for release

I did not understand this

Is there anyone to help me understand this

I did not know how to install it

It keep saying * sets command was denied in console on my server. I named it uptime and did
add_ace resource.uptime command.sets allow

can you provide some guidance on how you got it working?

@Hawaii_Beach maybe you could lend some knowledge