PiTyUs.Hire me

Engineering

Running tasks on a schedule in FiveM

Run code on a schedule in FiveM: repeating loops with Wait, SetTimeout for one-off delays, clock-based jobs with os.date on the server, ox_lib’s lib.cron.new with cron expressions, reacting to txAdmin scheduled restarts, and avoiding drift, duplicates and heavy work on the main thread.

Updated 8 min readBy PiTyUs · FiveM developer

Overview

Paychecks every 15 minutes, a daily shop restock at midnight, weekly leaderboard resets — servers are full of scheduled work. FiveM gives you simple loops and timeouts, and ox_lib adds real cron expressions for clock-based jobs.

Intervals and delays

server.lualua
-- every 15 minutes
CreateThread(function()
    while true do
        Wait(15 * 60 * 1000)
        -- pay salaries
    end
end)

-- once, 10 seconds from now
SetTimeout(10000, function()
    print('ten seconds later')
end)

Clock-based jobs with ox_lib

server.lua (with @ox_lib/init.lua)lua
-- every day at 06:00 server time
lib.cron.new('0 6 * * *', function()
    -- restock shops
end)

-- every Monday at 00:00
lib.cron.new('0 0 * * mon', function()
    -- reset weekly leaderboard
end)

-- every 5 minutes
lib.cron.new('*/5 * * * *', function()
    -- sync something
end)

Expressions are minute hour day month weekday, with *, lists (1,2,3), ranges (1-5), steps (*/5) and short weekday names. Times follow the server machine’s clock. More on ox_lib: ox_lib guide.

Restarts and missed runs

  • Store the last run time in the database; on start, run a job that was missed while the server was down if it matters (daily rewards).
  • Guard against double runs when a resource restarts during the job minute.
  • Save player data before scheduled restarts using txAdmin’s events — see scheduled restarts.

Heavy jobs

A job that updates every row in a table blocks nothing if it uses awaited queries, but large synchronous work still causes hitches. Split big jobs into batches with short waits between them — see Lua performance.

Frequently asked questions

How do I run a function every hour in FiveM?

Use a thread with Wait(3600000) in a loop, or lib.cron.new('0 * * * *', fn) with ox_lib for on-the-hour runs.

Does FiveM have cron jobs?

ox_lib provides lib.cron.new with standard cron expressions on the server.

Which time zone does lib.cron use?

The server machine’s local time.

How do I run something before a restart?

Listen for txAdmin:events:scheduledRestart and act when secondsRemaining is low.

Need this built, not just explained?

Ten years of FiveM work, from Lua to NUI

Custom resources, React NUI, ESX / QBCore / Qbox integration, OneSync performance audits and security reviews — plus the websites and SEO around your server brand.

Related guides