Skip to content

Interaction & availability ​

Availability ​

An NPC is available to a player when all of these hold:

  1. it is enabled,
  2. its routingBucket is unset or equals the player's bucket (networked NPCs: bucket 0 when unset),
  3. its conditions match.
Local NPC Networked NPC
Available Spawned on that client inside spawnDistance Shown on that client while the server has it spawned
Not available Hidden at once, deleted after despawnDelayMs The server only spawns it for players matching jobs / gangs. A client outside the hours, job or gang hides it locally every frame, lets the player pass through it, removes its interact option and does not task it

The server pushes each player's routing bucket to them every spawn.bucketPollIntervalMs (2 s). After SetPlayerRoutingBucket, call refreshPlayer(source) for an instant switch.

Conditions are not security

Conditions decide what a player is shown. A modified client can show itself anything. Validate on the server whatever an NPC leads to.

Conditions ​

lua
jobs  = { police = 0, sheriff = 2 },   -- name → minimum grade
gangs = { 'ballas' },                   -- a plain list means grade 0
hours = { from = 22, to = 6 },          -- in-game clock
Field Rules
jobs The player's job is in the table and their grade is at least the minimum. Names: letters, digits and . _ : -, up to 64 characters. Grades 0 to 1000. At most definition.maxConditionEntries (32) names
gangs The same for the gang. When both jobs and gangs are set, matching either one is enough, as in np_prophandler
hours from and to are whole hours 0 to 23. from is included, to is not. { from = 22, to = 6 } wraps past midnight. from == to means always. Checked on the client only: the server has no in-game clock

Job and gang come from the framework bridge: es_extended (gang from xPlayer.gang or job2), then qbx_core, then qb-core. The client re-reads them every 10 seconds and on esx:playerLoaded, esx:setJob, esx:setJob2, QBCore:Client:OnPlayerLoaded, QBCore:Client:OnJobUpdate, QBCore:Client:OnGangUpdate and qbx_core:client:playerLoaded. Without a framework, NPCs with jobs or gangs are never available.

The interact option ​

interact is one option, or a list of up to definition.maxInteractOptions (6) options with unique names.

lua
-- one option
interact = { label = 'Talk', icon = 'comments', key = 'E', distance = 2.0, event = 'np_gangs:lester:open' }

-- a list
interact = {
  { name = 'buy', label = 'Buy', icon = 'cart-shopping', event = 'np_drugs:dealer:use', server = true },
  { name = 'rob', label = 'Rob', icon = 'gun', key = 'G', event = 'np_drugs:dealer:use', server = true,
    hours = { from = 20, to = 4 } },
  { name = 'search', label = 'Search', icon = 'magnifying-glass', key = 'H', event = 'np_drugs:dealer:use',
    server = true, jobs = { police = 0, sheriff = 0 } },
}
Field Default Notes
name its position ('1', '2', …) Letters, digits and . _ : -, up to 32 characters. Unique per NPC. Sent with the event
label required Up to 64 characters
event required resource:name form. It must start with the owner's resource name and a colon, unless it is listed in interactEvents
icon 'comments' Font Awesome name without prefix, or with fa- / fas
key 'E' One of E F G H X Y Z B K L U. Used by the np_hud and built-in prompts. See Keys
distance 2.0 0.5 to 10
server false Fire a server event instead of a client event
minigame none Server registrations only. See Minigames
jobs, gangs, hours none Per option, the same shape as on the NPC (and as np_prophandler's options), checked on top of the NPC's own conditions
canInteract none Client definitions only (addLocalNpc, createLocalPed, createNetworkedPed): function(entity, id, option, data) returning true to show the option. See Client predicates

Validation reasons: 'invalid_interact', 'too_many_interact_options', 'duplicate_interact_name', 'interact_event_not_allowed', 'invalid_jobs', 'invalid_gangs', 'invalid_hours', 'can_interact_client_only', 'invalid_can_interact', 'minigame_server_only'.

The event ​

When the player uses an option:

  • Client event (default): TriggerEvent(event, id, entity, data, receipt, option).
  • server = true: TriggerServerEvent(event, id, netId, data, receipt, option). Networked peds send their net id, local peds 0. Your server handler must validate the request itself.
  • Then the local event np_npchandler:interact fires with (id, entity, data, option), and speech.interact plays if set.

receipt is set only for an option with a minigame. option is the option's name ('1' for a single unnamed option), so one event can serve several options. For immediate peds without an id, id is the ped handle. Availability, the option's conditions and its predicates are checked again when the option is used.

lua
-- client
AddEventHandler('np_identification:desk:use', function(id, entity, data, receipt, option)
  OpenDesk(data.desk)
end)

-- server, with server = true
RegisterNetEvent('np_drugs:dealer:use', function(id, netId, data, receipt, option)
  local src = source
  -- option = 'buy' | 'rob' | 'search'. Conditions are presentation: check the job again here
end)

Keys ​

Options may share a key. When two shown options have the same key (for example both default to E), the np_hud and built-in prompts mark the selected one with >, and the arrow keys up and down (controls 172 / 173) move the selection; the key then fires the selected option. Give options different keys to avoid the selection altogether, as np_prophandler requires.

How several options are shown ​

Provider Several options
np_hud / fallback The nearest NPC with an available option in reach gets the prompt. It lists every available option within that option's distance as [KEY] label (np_hud shows four rows at a time). Each key fires its own option
ox_target / qb-target One target option per interact option, each with its own distance and conditions (canInteract). Target names are np_npchandler:<key> for the first option and np_npchandler:<key>:<name> for the others

Client predicates ​

client

A predicate hides an option per player, for example while the player is dead, cuffed or has a menu open. Functions stay on the client: a server registration with canInteract is refused ('can_interact_client_only'). Client definitions take canInteract per option. For server NPCs, attach one with setCanInteract:

lua
-- client
exports.np_npchandler:setCanInteract('np_drugs:dealer_3', 'rob', function(entity, id, option, data)
  return IsPedArmed(PlayerPedId(), 4)
end)
exports.np_npchandler:setCanInteract('np_drugs:dealer_3', nil, fn)   -- every option of the NPC
exports.np_npchandler:setCanInteract('np_drugs:dealer_3', 'rob', nil) -- remove
  • A predicate runs on the slow interaction pass (np_hud, fallback) or when the target script asks (ox_target, qb-target), never every frame from the handler.
  • It must return true. An error counts as false and is logged with debug = true.
  • Predicates belong to the resource that attached them and go when it stops. The NPC does not have to exist yet. Only that resource may replace or remove them ('not_owner').

Predicates, like conditions, are presentation only. Check again on the server.

Minigames (np_minigames) ​

interact.minigame puts a skill game from np_minigames in front of an option: pickpocketing a dealer (strike), persuading a guard (skillcheck), robbing a clerk. np_minigames judges it on the server; nothing the client says about the outcome is trusted. Only server registrations (registerNpc, registerDefinition) can have one; addLocalNpc and immediate peds answer minigame_server_only.

Field Default Notes
game required np_minigames game id (strike, skillcheck, lockpick, … see Games & difficulty)
difficulty 'medium' easy, medium, hard, expert
opts none Game params ({ rounds = 2 }), plus label / sub / icon / tint. A small plain table
item none 'lockpick' or { name, count, require, remove = 'never' | 'fail' | 'success' | 'always', chance, degrade } (np_minigames items)
cooldownMs 0 Per-player time between two games
label interact label Text on the game card
fireOnFail false Fire the event after a lost game too (with its receipt), e.g. for an alarm

np_npchandler registers an np_minigames profile per gated option: np_npchandler:npc:<id> for the first option, np_npchandler:npc:<id>:<n> for option n of a list. The client plays it and, on a win, fires your event with the receipt as the 4th argument and the option name as the 5th: (id, entity, data, receipt, option) or, with server = true, (id, netId, data, receipt, option). Check it on your server:

lua
exports.np_npchandler:registerNpc({
  id = 'np_drugs:dealer_3', model = 'g_m_y_ballasout_01', coords = vector4(...), scenario = 'WORLD_HUMAN_SMOKING',
  interact = { label = 'Pickpocket', icon = 'hand', event = 'np_drugs:dealer:pickpocket', server = true,
    minigame = { game = 'strike', difficulty = 'hard', cooldownMs = 60000, fireOnFail = true } },
})

RegisterNetEvent('np_drugs:dealer:pickpocket', function(id, netId, data, receipt, option)
  local src = source
  local result, err = exports.np_npchandler:redeemInteraction(src, id, receipt, option)
  if not result then return end                       -- forged, reused, expired, too far
  if result.success then GiveStolenCash(src) else AlertDealer(src, id) end
end)

redeemInteraction(src, id, receipt, option?) (owner resource only) checks the routing bucket and the distance (interact.distance plus the wander / guard radius for local NPCs + minigames.distanceTolerance), then spends the receipt with np_minigames' Redeem. It answers np_minigames' result (success may be false with fireOnFail), or { success = true, skipped = true } when no game was needed: no minigame on the option, np_minigames not running, minigames.enabled off, or the profile could not be registered (a warning is printed; the NPC then works without a game). With option only that option's game counts (an option without one answers skipped). Without it every gated option of the NPC is tried, since np_minigames refuses a receipt of another profile without spending it. Errors: unknown_npc, not_owner, unknown_option, wrong_bucket, too_far, no_ped, and np_minigames' no_receipt, used, expired, wrong_player, wrong_profile. Calling it in every handler of a gated NPC keeps the event safe whether np_minigames runs or not. Profiles are registered again when np_minigames (re)starts and removed with the NPC.

Providers ​

The options are shown by the first provider that runs, with interaction.provider = 'auto':

Provider How
np_hud np_hud's interaction prompt for the nearest available NPC in range, one row per option; np_npchandler reads the keys
ox_target addLocalEntity options on the ped
qb-target AddTargetEntity options on the ped
fallback 3D text with the key over the ped, only with interaction.fallback = true
none No option. Used when nothing above runs and fallback is off

The provider is picked when an NPC's option is added (when the ped spawns or streams in). Force one with interaction.provider. Per-frame work only runs while a prompt is shown.