Skip to content

Immediate peds ​

Peds created now for the caller and not streamed: preview peds of a scene, customers that walk up and leave, a k9 dog, mission peds, a player's body. They take the same definition as registered NPCs; id is optional. The client exports can clone a ped's look instead of loading a model. Everything a resource creates is deleted when that resource stops.

Export Side Creates Owned by
createLocalPed client A local ped on this client The calling resource
createNetworkedPed client A networked ped this client creates (the k9 pattern) The calling resource and this player
createNetworkedPedInVehicle client The same, in a vehicle seat The calling resource and this player
spawnNetworkedPed server A networked ped the server creates The calling resource, optionally a player
spawnPedInVehicle server The same, in a vehicle seat The calling resource, optionally a player

Client creation never blocks ​

client

The client exports never wait inside the export. Exports cannot wait across resources, and the model has to load first. They return at once:

lua
local handle, entity = exports.np_npchandler:createLocalPed(def, callback)
-- handle: 'ped:<n>', entity: 0 while the ped is being built
-- or nil, reason when the definition is refused

The result arrives three ways, all with the same arguments (handle, entity, netId, reason):

  1. the optional callback,
  2. the local event np_npchandler:pedReady,
  3. getPedEntity(handle), which returns entity, netId (0 until the ped exists).

entity = 0 in the callback means the build failed, and reason says why. netId is nil for local peds.

Failure reason Meaning
owner_stopped The calling resource stopped (or the handle was deleted) during the build
model_missing, model_timeout, model_cancelled The model is not in the game files, did not load in streaming.modelLoadTimeoutMs, or the build was cancelled
create_ped_failed CreatePed / CreatePedInsideVehicle returned no ped
ticket_timeout, ticket_refused, client_ped_limit Networked: no creation ticket from the server
network_timeout Networked: the ped did not become networked within streaming.networkTimeoutMs
register_… Networked: the server refused the registration, e.g. register_invalid_ticket, register_not_entity_owner, register_entity_unavailable, register_timeout

Refusals returned by the export itself: 'resource_disabled', 'invalid_callback', the validation reasons, 'local_ped_limit_reached', 'networked_ped_limit_reached' and 'invalid_vehicle_or_seat'.

Preview peds ​

client

lua
local handle = exports.np_npchandler:createLocalPed({
  model = 'mp_m_freemode_01',
  coords = vector4(402.9, -996.5, -100.0, 180.0),
  placeOnGround = true,
  anim = { dict = 'anim@heists@heist_corona@single_team', clip = 'single_team_loop_boss', flag = 1 },
}, function(handle, ped, _, reason)
  if ped ~= 0 then Scene.focus(ped) end
end)

exports.np_npchandler:deleteLocalPed(handle) -- the handle or the entity
  • Local peds stay frozen unless frozen = false or they have a wander, patrol or guard preset.
  • They count against streaming.maxLocalPeds (48) together with streamed NPCs.
  • Their interact option or list (with canInteract), held props and preset work as for registered NPCs.

Customers that walk up and leave ​

lua
local handle = exports.np_npchandler:createLocalPed({ model = m, coords = vector4(x, y, z, 0.0), frozen = false },
  function(handle, ped)
    if ped ~= 0 then exports.np_npchandler:walkTo(handle, corner, { speed = 1.0 }) end
  end)

-- later
exports.np_npchandler:dismissPed(handle, { wander = true, deleteAfterMs = 20000 })

dismissPed(target, options) unfreezes the ped, clears its tasks, lets it wander off (unless wander = false) and deletes it after deleteAfterMs (default 20000, at most 300000).

Networked peds from the client ​

client

For peds the caller tasks itself, such as a k9 dog:

lua
local dogHandle = exports.np_npchandler:createNetworkedPed({
  model = 'a_c_shepherd',
  pedType = 28,
  coords = vector4(x, y, z, heading),
  invincible = true,
}, function(handle, dog, netId, reason)
  if dog == 0 then return print('k9 failed: ' .. reason) end
  TaskFollowToOffsetOfEntity(dog, PlayerPedId(), 0.6, -1.0, 0.0, 3.0, -1, 1.5, true)
  TriggerServerEvent('np_drugs:k9:dog', netId)
end)

-- on dismiss
exports.np_npchandler:deleteNetworkedPed(dogHandle)

createNetworkedPedInVehicle(vehicle, seat, def, callback) puts the ped in a seat (-1 is the driver, up to 15). It is networked when the vehicle is networked, otherwise local. coords is optional there.

How it works:

  1. The client asks the server for a single-use ticket, bound to this player and valid for 15 s. At most 16 outstanding tickets and client peds per player.
  2. The client loads the model, creates the ped, applies the definition and waits until it is networked.
  3. The client registers the net id with the ticket. The server checks that the entity is a ped this player owns and that no handler ped uses it, retrying for about 3 s until the entity arrives, then acknowledges.
  4. The callback runs with the entity and net id. Held props are attached and the interact option is added.

Afterwards:

  • Deleting goes through the server. deleteNetworkedPed and dismissPed ask the server, so the ped is removed even after its ownership migrated to another client. The server deletes the ped when the player drops, and tells the creator when the ped is gone.
  • Networked immediate peds are not frozen unless frozen = true.
  • At most 16 per client.
  • Other clients render their held props from the server-written state bag np_npchandler:spec. The interact option and data stay with the creator.

Clone a ped ​

client

clone = 'player' (or any ped handle) on createLocalPed, createNetworkedPed or createNetworkedPedInVehicle copies that ped's look instead of loading a model. np_spy-pigeon uses it for the body the player leaves behind while they fly:

lua
exports.np_npchandler:createNetworkedPed({ clone = 'player', coords = vector4(x, y, z, h), frozen = true,
  invincible = false, scenario = 'PROP_HUMAN_SEAT_CHAIR_MP_PLAYER' }, function(handle, body, netId, reason)
  if body == 0 then return print('body failed: ' .. reason) end
  Pigeon.body, Pigeon.bodyHandle = body, handle
end)
-- now swap the player's model; the clone was taken inside the export
exports.np_npchandler:deleteNetworkedPed(Pigeon.bodyHandle)
  • Inside the export a hidden local copy is taken with ClonePed (model, components, props, head blend), plus face features, head overlays and their colours, hair and eye colour and tattoos. The look is the one at call time, even when the caller changes the player's model right after.
  • A local clone becomes that copy. A networked clone is cloned from it once the server ticket arrives, and the copy is deleted.
  • model may be left out: it is the source's (a different one answers 'clone_model_mismatch'). coords is still required.
  • The outfit is kept: outfit defaults to default = false and only changes the slots it lists.
  • Everything else (scenario, flags, held props, interact, ownership, deletion when the caller stops) is the same as for any immediate ped.
  • Registrations and server peds refuse it ('clone_not_supported_here'). Other errors: 'invalid_clone', 'clone_source_missing', 'clone_failed'.

Server mission peds ​

server

lua
local netId, entity = exports.np_npchandler:spawnNetworkedPed({
  model = 'g_m_y_lost_01',
  coords = coords,
  weapon = 'WEAPON_PISTOL',
  relationship = { group = 'NP_TRIAL_HOSTILE', hates = { 'PLAYER' } },
  accuracy = 25,
  ownerSource = src,
})

local copNet = exports.np_npchandler:spawnPedInVehicle(vehicleNetId, -1, { model = 's_m_y_cop_01' })

exports.np_npchandler:deleteNetworkedPed(netId)
  • Both return netId, entity at once, or nil, reason. The server creates the ped synchronously.
  • They count against spawn.maxNetworkedPeds ('networked_ped_limit_reached') and need OneSync ('onesync_required').
  • ownerSource must be a connected player. The ped is deleted when that player drops, takes their routing bucket unless routingBucket is set, and may be tasked by that player.
  • spawnPedInVehicle takes the vehicle's routing bucket. coords is optional.
  • Mission peds are not frozen unless frozen = true.
  • The spec (look, flags, behaviour, held props) is written to the entity's state bag np_npchandler:spec. The client that controls the ped applies it, and every client renders the held props.
  • Only the owning resource can delete one ('unknown_ped', 'not_owner').

Other refusals: 'resource_disabled', 'spawning_disabled', 'invalid_owner_source', 'create_ped_failed', 'network_id_unavailable', 'invalid_vehicle_or_seat', 'vehicle_unavailable' and the validation reasons.