Skip to content

Local NPCs ​

A local NPC (mode = 'local', the default) is a client-local ped. Every client that should see it creates its own copy while the player is near and deletes it after the player has left. It costs no network entity. Use it for shopkeepers, desk clerks, dealers and decoration.

From the server ​

server

Every player gets it:

lua
local ok, reason = exports.np_npchandler:registerNpc({
  id = 'np_gangs:lester',
  model = 'ig_lestercrest',
  coords = vector4(1273.9, -1719.7, 54.77, 25.0),
  scenario = 'WORLD_HUMAN_CLIPBOARD',
  spawnDistance = 60.0,
  interact = { label = 'Talk', icon = 'comments', event = 'np_gangs:lester:open' },
})
if not ok then print('lester refused: ' .. reason) end

Register many at once with registerNpcs. It sends one state message for the whole list. updateNpc replaces a definition and respawns the NPC with it. setNpcEnabled hides it without removing it.

From the client ​

client

When only this player should have it:

lua
exports.np_npchandler:addLocalNpc({
  id = 'np_drugs:rehab_doctor',
  model = 's_m_m_doctor_01',
  coords = vector4(-475.2, -356.9, 34.1, 80.0),
  scenario = 'WORLD_HUMAN_CLIPBOARD',
  interact = { label = 'Ask for help', icon = 'handshake-angle', event = 'np_drugs:rehab:open', distance = 2.5 },
})

exports.np_npchandler:removeLocalNpc('np_drugs:rehab_doctor')
  • Client NPCs are always local. mode = 'networked', networked, persistence and alwaysSpawned are refused with 'mode_not_supported_here'.
  • They belong to the calling resource and are removed when it stops.
  • Server and client NPCs share one id space on each client. A client id that is already taken returns 'id_in_use', and a server NPC replaces a client NPC with the same id. Prefix ids with your resource name.

Distance tiers ​

Each client pass compares the player's position with every local NPC in the nearby grid cells. Distances are 3D.

 player ── renderDistance ── spawnDistance ── + despawnMargin ──────────▶
 │ shown │ hidden, kept      │ (hysteresis)  │ hidden at once, deleted
 │       │ (render tier)     │               │ after despawnDelayMs
Tier When What happens
Spawn Within spawnDistance (default 60.0) and available The model is loaded and the ped created, dressed and given its idle task. Nearest first, at most streaming.maxSpawnsPerTick (2) per pass
Render Optional renderDistance, below spawnDistance. Beyond it The ped is hidden (invisible, no collision, frozen) but never deleted for it. Inside renderDistance it is shown again
Despawn margin Between spawnDistance and spawnDistance + streaming.despawnMargin (10 m) A spawned ped stays
Delayed delete Beyond spawnDistance + despawnMargin, or no longer available Hidden at once, deleted after despawnDelayMs (default streaming.despawnDelayMs, 10 s). Coming back before that shows the same ped again

A local NPC whose definition changed or that was removed is deleted at once. A ped deleted by something else is created again on the next pass.

Budgets ​

  • streaming.maxLocalPeds (48) caps the local peds per client, streamed NPCs and createLocalPed peds together.
  • A pass is skipped while the player stays within streaming.moveThreshold (2 m) of the last scanned position and nothing changed. A full pass still runs every streaming.revalidateIntervalMs (10 s).
  • While nothing is registered, created or overridden, the loop runs every streaming.idlePollIntervalMs (2 s) instead of streaming.pollIntervalMs (500 ms).

Models ​

  • One ref-counted loader for models and anim dicts.
  • IsModelInCdimage is checked first. A model missing from the game files is never requested again.
  • A model that does not load within streaming.modelLoadTimeoutMs (5 s) is tried again after streaming.retryDelayMs (30 s).

NPC definition ​

The same fields are used by every way to create an NPC. Unknown fields are rejected with 'unknown_field'. Limits are in Config.NpcHandler.definition.

Field Default Notes
id required Letters, digits and . _ : -, up to 64 characters. Optional for immediate peds
model required Model name or hash. Optional restriction: restrict.models
coords required vector3, vector4 or { x, y, z, w / heading }. Optional for peds created in a vehicle
heading from coords.w
pedType 4 0 to 30. 28 for animals
mode 'local' 'local' or 'networked'. Networked only for server registrations
alwaysSpawned false Networked: exists without players near. See Networked NPCs
networked, persistence v0.2.0 compatibility: networked = true means networked; persistence = 'anchor' means networked + alwaysSpawned
routingBucket all buckets (local) / 0 (networked) Only players in this bucket get the NPC
spawnDistance 60.0 local / 120.0 networked 5 to 400
renderDistance none Local: 1 to spawnDistance. Hidden beyond it, never deleted for it
despawnDelayMs config 0 to 300000. Delay between leaving range and deletion
placeOnGround false Probe the ground below coords (within 3 m) before creating
enabled true
jobs, gangs none { police = 0, sheriff = 2 } (name → minimum grade) or { 'police' }. See Conditions
hours none { from = 22, to = 6 }, in-game hours, wraps past midnight
role none Free-form safe id. Optional restriction: restrict.roles
outfit default variation See Outfit
props none Held props, up to 4. See Held props
scenario none Any native scenario name, or a v0.2.0 alias. Not together with anim
anim none { dict, clip, flag = 1 }. Not together with scenario
weapon none 'WEAPON_PISTOL' or { name, ammo = 250, visible = true }
invincible true
frozen true, false with wander / patrol / guard Immediate networked peds and peds in vehicles default to false
blockEvents true Also turns flee attributes off
ragdoll false
collision true
relationship none 'GROUP' or { group, hates = { 'PLAYER' }, likes = {} }
health, armor game 101 to 10000; 0 to 100
accuracy, combatAbility, combatAttributes, dropsWeapons game / false 0 to 100; 0 to 2; { [46] = true }; boolean
voice game Ambient voice name
speech none { greet, interact, params = 'SPEECH_PARAMS_FORCE_NORMAL_CLEAR', greetDistance = 4.0 }
wander, patrol, guard none Behaviour presets, only one of them. See Presets
interact none One option or a list of up to 6. Client definitions may add canInteract. See Interaction
clone none 'player' or a ped handle: copy that ped's look. Client immediate peds only, see Clone a ped
respawn config Networked only. See Respawn
perception, policies, dialogueSet, interactionIds, perceptionInteractionId none Reactions, networked only. See Reactions & incidents
ownerSource none Server immediate peds only. See Immediate peds
allowRemoteControl false Networked: lets other clients task the ped. See Runtime control
data none Small table passed to spawned and interact events: 4 levels deep, 32 entries, strings up to 256 characters

Validation reasons include 'definition_not_table', 'unknown_field', 'invalid_id', 'invalid_model', 'model_not_allowed', 'invalid_ped_type', 'invalid_coords', 'coords_out_of_bounds', 'invalid_heading', 'invalid_mode', 'mode_not_supported_here', 'invalid_routing_bucket', 'invalid_spawn_distance', 'invalid_render_distance', 'invalid_despawn_delay', 'invalid_flag', 'invalid_jobs', 'invalid_gangs', 'invalid_hours', 'invalid_role', 'role_not_allowed', 'invalid_outfit', 'invalid_props', 'invalid_scenario', 'scenario_not_allowed', 'invalid_anim', 'conflicting_idle', 'invalid_weapon', 'invalid_relationship', 'invalid_health', 'invalid_armor', 'invalid_accuracy', 'invalid_combat_ability', 'invalid_combat_attributes', 'invalid_voice', 'invalid_speech', 'invalid_wander', 'invalid_patrol', 'invalid_guard', 'conflicting_behavior', 'invalid_interact', 'too_many_interact_options', 'duplicate_interact_name', 'can_interact_client_only', 'interact_event_not_allowed', 'invalid_respawn', 'respawn_requires_networked', 'perception_required', 'perception_requires_networked', 'invalid_owner_source', and 'invalid_data_…' for a bad data table.

Rules ​

  • Don't delete peds you get from the handler. Remove or disable the NPC instead.
  • np_npchandler:despawned is raised before the ped is deleted, so the handle is still valid in the handler.
  • Everything a resource registered is removed when that resource stops. When np_npchandler restarts, register again (see Installation).