Skip to content

Runtime and Events

Scheduler

Client / Server

FunctionReturnsPurpose
CreateThread(callback)task IDStarts a managed coroutine. Alias: Citizen.CreateThread.
Wait(milliseconds)noneYields the current managed coroutine. Alias: Citizen.Wait.
SetTimeout(milliseconds, callback)task IDRuns once after a delay.
ClearTimeout(taskId)booleanCancels a scheduled task.
SetInterval(milliseconds, callback)task IDRuns repeatedly.
ClearInterval(taskId)booleanCancels an interval.
Schedule(expression, callback)task IDRuns a five- or six-field cron expression.
lua
CreateThread(function()
  while true do
    Wait(1000)
    print('tick')
  end
end)

Schedule('0 */5 * * * *', function()
  print('every five minutes')
end)

Avoid unbounded Wait(0) loops. The runtime enforces queue, callback, wakeup, and CPU budgets per resource.

Events

FunctionSidePurpose
AddEventHandler(name, callback)Client / ServerHandles a local event.
RegisterNetEvent(name, callback)Client / ServerHandles local and network calls.
TriggerEvent(name, ...)Client / ServerEmits on the current process.
TriggerServerEvent(name, ...)ClientSends JSON-serialized arguments to the server.
TriggerClientEvent(name, peerId, ...)ServerSends arguments to one client.

Custom event names are case-insensitive. Network handlers receive the sender in global source.

Commands and permissions

lua
RegisterCommand(commandName, callback, restricted)
Command.Execute(rawCommand, sourcePeer)
HasPermission(permission)
Player.HasPermission(peerId, permission)
IsPlayerAceAllowed(source, ace)
IsPlayerAdmin()

restricted may be false, true for command.<name>, or an explicit ACE string. Permission-sensitive logic belongs on the server.

Exports

lua
exports('getState', function()
  return { active = true }
end)

local state = exports['other_resource'].getState()

Exports are synchronous and stay on the current side. A missing or failing export returns nil and writes an error to the log.

Convars

lua
GetConvar(key, defaultValue)
GetConvarInt(key, defaultValue)
GetConvarFloat(key, defaultValue)
GetConvarBool(key, defaultValue)
SetConvar(key, value)
SetReplicatedConvar(key, value)

Resource-local variants use the GetResourceConvar* and SetResourceConvar* prefixes. Only server values are authoritative; setr values are replicated to clients, while sets values remain engine-only secrets.

Lua resources for Valheim, built on a server-authoritative runtime.