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