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.
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
-- 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
-- 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
- FrameworksGetting started with ox_libStart ox_lib before your resource, add shared_script '@ox_lib/init.lua' and lua54 'yes' to your fxmanifest, and the global lib becomes available. Use cache.ped/cache.vehicle instead of calling natives in loops, lib.callback for client↔server requests, lib.notify, lib.progressBar, lib.registerContext/lib.showContext, lib.inputDialog, lib.points and lib.zones for interaction, and lib.addCommand for commands with ACE restrictions.
- EngineeringThreads and Wait in FiveM: loops that do not eat framesCreateThread(fn) starts a coroutine that runs alongside the game; Wait(ms) pauses it and lets everything else run. Wait(0) resumes on the next frame, so the loop runs every frame (60+ times a second). Use it only while you must draw or read input every frame; otherwise sleep for hundreds of milliseconds, and make loops adaptive — fast when the player is near something, slow when they are not.
- EngineeringOptimising FiveM Lua scriptsSleep loops dynamically (long Wait when nothing is nearby, Wait(0) only when you must draw or read input), replace polling with events and state bags, measure distance with #(a - b) on vectors, read values like the player ped once per tick (or from ox_lib’s cache), use backtick hashes, avoid creating tables and strings in hot loops, and batch database writes. Measure before and after with resmon and the profiler.
- EngineeringDesigning a database for a FiveM serverGive every table a primary key, reference characters by one stable identifier (citizenid on QBCore, identifier on ESX), index every column you search or join on, use utf8mb4 so names with emoji work, store small flexible data as JSON but put anything you search or count in real columns, and add created_at/updated_at timestamps.