Skip to content

Exports ​

lua
local npcs = exports.np_npchandler
local ok, reason = npcs:registerNpc({
  id = 'my_resource:clerk', model = 'mp_m_shopkeep_01', coords = coords,
})
  • Everything you register or create belongs to your resource (GetInvokingResource()). Only your resource can change or remove it, and it is removed when your resource stops.
  • Exports that can fail return false, reason or nil, reason. The reasons are strings such as 'invalid_coords'; the NPC definition lists the validation reasons.
  • Exports from a resource that isn't running throw. If np_npchandler is optional for you, check GetResourceState('np_npchandler') == 'started' or wrap the call in pcall.

Server ​

server

NPCs ​

Export Returns Description
registerNpc(def) true or false, reason Register a local or networked NPC
registerNpcs(defs) count, failures Register a list in one state message. Valid entries are registered even when others fail. failures maps the list index to the reason
updateNpc(id, def) true or false, reason Replace your NPC with a full new definition and respawn it. def.id must equal id
removeNpc(id) true or false, reason Remove your NPC
setNpcEnabled(id, enabled, token?) true or false, reason Enable or disable your NPC. The token is only needed for NPCs from registerDefinition
redeemInteraction(src, id, receipt, option?) result or nil, reason Spend the np_minigames receipt of an interact.minigame on your NPC. option is the event's 5th argument: only that option's game counts
getNpc(id) table or nil The public definition plus owner, netId and lifecycle
getNpcState(id) table or nil Runtime state: the v0.2.0 shape plus mode, owner, lastError, spawnAttempts, lastPolicyResult, lastIncident
getNpcEntity(id) entity, netId A networked NPC's ped while it exists, else 0, 0
refreshPlayer(source) boolean Push the player's routing bucket now instead of within spawn.bucketPollIntervalMs (2 s). Call it after SetPlayerRoutingBucket. false for an unknown player

Reasons besides validation: 'id_in_use', 'definition_limit_reached', 'unknown_npc', 'not_owner', 'definition_in_use' (update during a reaction), 'definition_id_mismatch', 'invalid_enabled', 'owner_not_started', 'invalid_invoking_resource', 'policy_dependency_unavailable', 'interaction_handler_unavailable', 'interaction_handler_owned_elsewhere'.

lua
-- qbx: a guard only police see, at the station door
local ok, reason = exports.np_npchandler:registerNpc({
  id = 'np_police:door_guard',
  model = 's_m_y_cop_01',
  coords = vector4(441.1, -982.0, 30.7, 90.0),
  mode = 'networked',
  jobs = { police = 0 },
  guard = { radius = 4.0 },
})

exports.np_npchandler:setNpcEnabled('np_police:door_guard', false)

Mission peds ​

Export Returns Description
spawnNetworkedPed(def) netId, entity or nil, reason Create a networked ped now. See Server mission peds
spawnPedInVehicle(vehicleNetId, seat, def) netId, entity or nil, reason The same, in a seat (-1 driver, up to 15). Takes the vehicle's routing bucket
deleteNetworkedPed(netId) true or false, reason Delete your mission ped. Reasons: 'unknown_ped', 'not_owner'

Population ​

Synced to every client. See Ambient population.

Export Returns
setPopulation(id, values, area?) true or false, reason
suppressArea(id, options) true or false, reason
setScenarioTypes(id, map) true or false, reason
setScenarioGroups(id, map) true or false, reason
clearPopulation(id) boolean (something was removed)

Reactions (v0.2.0) ​

Export Returns
registerDefinition(def, context?) token, state or nil, reason
updateDefinition(id, def, token) true, state or false, reason
removeDefinition(id, token) true or false, reason
registerPolicy(id, adapter) / registerInteractionHandler(id, adapter) token or nil, reason
updatePolicy(id, adapter, token) / updateInteractionHandler(id, adapter, token) true or false, reason
removePolicy(id, token) / removeInteractionHandler(id, token) true or false, reason

See Reactions & incidents.

Diagnostics ​

getDiagnostics() on the server returns enabled, spawningEnabled, runtimeAvailable, runtimeReason, resourceVersion, epoch, revision, definitions, byMode, byOwner, policies, interactionHandlers, incidentDedupEntries, networked (networked, spawned, dormant, anchors, failed, inScope), immediate (server, client, tickets), activeNetworkedPeds, maxNetworkedPeds, population, entities (spawned, despawned, rehydrated, failures, immediateSpawned, immediateDeleted), counters (registered, updated, removed, rejected) and states (one entry per NPC with id, owner, mode, lifecycle, netId, generation, lastError, playersInScope, …).

Client ​

client

NPCs ​

Export Returns Description
addLocalNpc(def) true or false, reason A client-only local NPC. Reasons besides validation: 'resource_disabled', 'definition_limit_reached', 'id_in_use'
addLocalNpcs(defs) count, failures failures maps the list index to the reason
removeLocalNpc(id) true or false, reason Reasons: 'unknown_npc', 'not_owner'
setLocalNpcEnabled(id, enabled) true or false, reason Reasons: 'unknown_npc', 'not_owner', 'invalid_flag'
getNpc(id) table or nil The definition with entity and spawned
getNpcState(id) table or nil Networked: the v0.2.0 shape with entityInScope. Local: lifecycle, hidden, entityInScope. Both with mode and available
getNpcEntity(id) entity or 0 Local or networked NPC entity. 0 while not spawned or still loading
isNpcSpawned(id) boolean
getSpawnedNpcs() { [id] = entity } Every spawned local and networked NPC, hidden ones included
setCanInteract(id, option, fn) true or false, reason Attach a client predicate fn(entity, id, option, data) to an NPC's option. option = nil: every option. fn = nil removes it. The NPC does not have to exist yet. Reasons: 'invalid_id', 'invalid_option', 'invalid_can_interact', 'not_owner'
requestInteraction(id, interactionId?) requestId or false, reason Send a reaction request. Reasons: 'resource_disabled', 'npc_unavailable', 'interaction_not_allowed', 'request_pending', 'cooldown', 'entity_not_in_scope'

Immediate peds ​

Export Returns Description
createLocalPed(def, callback?) handle, entity or nil, reason A preview ped. Never blocks: entity is 0 until built. def.clone copies a ped
createNetworkedPed(def, callback?) handle, entity or nil, reason A networked ped owned by this client. def.clone copies a ped
createNetworkedPedInVehicle(vehicle, seat, def, callback?) handle, entity or nil, reason The same, in a vehicle seat
getPedEntity(handle) entity, netId 0 while loading or after a failure
deleteLocalPed(target) / deleteNetworkedPed(target) true or false, reason target is the handle or the entity. Networked peds are deleted by the server. Reasons: 'unknown_ped', 'not_owner'
dismissPed(target, { wander = true, deleteAfterMs = 20000 }) true or false, reason Walks off, deleted after the delay (by the server for networked peds)

The callback and the np_npchandler:pedReady event get (handle, entity, netId, reason).

Runtime control ​

Export Returns
playAnim(target, dict, clip, { flag, duration }) true, true, 'routed' or false, reason
playScenario(target, scenario) same
speak(target, speech, params?) same
faceEntity(target, entity, durationMs) same
walkTo(target, coords, { speed }) same
stopTask(target) same
resumeBehavior(target) same

target is an NPC id, a ped handle or a handled ped entity. See Runtime control.

Population ​

setPopulation, suppressArea, setScenarioTypes, setScenarioGroups and clearPopulation, with the same arguments as on the server. They apply to this player only. See Ambient population.

Diagnostics ​

getDiagnostics() on the client returns enabled, epoch, revision, bucket, registered, localDefinitions, available, spawned, hidden, loading, failed, localPeds, maxLocalPeds, modelsLoading, missingModels, networked (tracked, inScope, specs, hidden, errors), immediate (local, networked, loading), population, populationFrameActive, interactProvider and interactOptions.