Skip to content

Using it from your resource ​

np_passwords keeps the secret. Your resource registers a lock once and later asks one question: did this player get in? It never stores, receives or compares a code. This page walks through the common patterns; the full signatures are in Exports.

1. Register the lock ​

server

Register at every start, ideally in onResourceStart or after you loaded your own data. RegisterLock is idempotent and keeps the existing code, so a restart never resets a code the owner chose.

lua
local function registerApartment(apt)
  exports.np_passwords:RegisterLock({
    id = ('apt%d:front'):format(apt.id),
    label = ('Apartment %d'):format(apt.id), icon = 'door-closed',
    type = 'pin', length = 4,
    owner = { type = 'char', id = apt.ownerCid },
    coords = apt.door, radius = 1.5,
    generate = true,                       -- first start: random code into the owner's keychain
    door = { model = `v_ilev_fa_frontdoor`, coords = apt.door, autoRelock = 8 },
    meta = { sub = apt.street },
  })
end

Calls made before np_passwords finished booting are queued, so start order between your resource and np_passwords does not matter for registration. When the apartment is sold, change the owner with another RegisterLock call and rotate the code with SetCode (or let the new owner set one with Create).

2. Ask for it ​

There are two ways. Both end with the server, not the client, deciding.

Client-initiated (most common) ​

The player interacts with your target / menu / zone; your client opens the prompt, then asks your server to act. Your server checks ConsumeUnlock, which np_passwords recorded when it saw the correct code:

lua
-- client
local ok, reason = exports.np_passwords:Prompt('apt12:safe')
if ok then TriggerServerEvent('apartments:openSafe', 12) end
lua
-- server
RegisterNetEvent('apartments:openSafe', function(apt)
  local src = source
  if not exports.np_passwords:ConsumeUnlock(src, ('apt%d:safe'):format(apt)) then return end   -- one-shot, 10 s
  exports.np_inventory:OpenInventory(src, 'stash', ('apt%d_safe'):format(apt))
end)

Never trust the ok your client sends: a modified client can send true. ConsumeUnlock (one-shot) or IsUnlocked(src, id, maxAge) (reusable within maxAge seconds) are the server-side proof.

Server-initiated ​

When the flow starts on the server, Prompt(src, id) opens the prompt on that client and yields until the player unlocks, cancels or Config.Prompt.timeout (120 s) runs out:

lua
-- server
RegisterNetEvent('garage:takeOut', function(plate)
  local src = source
  if not exports.np_passwords:Prompt(src, 'garage:' .. plate) then return end
  spawnVehicle(src, plate)
end)

Locks that open by themselves ​

You don't need either flow when np_passwords handles the target itself:

  • Doors (door = {…}): an unlock opens the door for everyone. See Doors.
  • Stashes (id = 'stash:<stashId>'): np_inventory / ox_inventory ask before the stash opens. See Stash locks.
  • Proximity prompts: every lock with coords gets E / G at its position. Leave coords out when your own target or menu opens the prompt.

3. React to other outcomes ​

lua
-- server
AddEventHandler('np_passwords:lockout', function(lockId, src, fails)
  if lockId:find('^bank:') then TriggerEvent('my_heists:alarm', lockId, src) end
end)

See Events for unlocked, failed, lockout, codeChanged, leaked and dispatch.

Let owners set the code ​

client

Create opens the generator for owners, managers (owner.grade and up, job bosses, vault owners) and staff with np_passwords.locks. Everyone else gets "You are not allowed to do that".

lua
-- client: from your apartment menu
local ok = exports.np_passwords:Create('apt12:front', {
  types = { 'pin', 'phrase' },     -- segmented control; the lock's type is preselected when allowed
  rules = { min = 6 },             -- merged with the lock's rules, the stricter value wins
})
if ok then TriggerServerEvent('apartments:codeChanged', 12) end   -- e.g. log it in your own system
Option Description
types Allowed types for the new code (pin, password, login, phrase, pattern). Default: the lock's type. Choosing another type changes the lock's type
type Preferred type, added to types
rules { min, max, words, digit, symbol, upper, noReuse }
length PIN length when the lock has none yet
sub Second line of the header

The new code goes to the creator's keychain (source created), vault items of the lock follow, every other keychain entry goes stale. From the server, set codes with SetCode(id, secret, bySrc?) instead.

Logins your resource keeps: verifiers ​

Some resources keep their own accounts (an MDT, a laptop OS, a bank portal). They can still use np_passwords' prompt, keychain and autofill without handing over the passwords. Register a verifier that checks the pair, then open a prompt with verify instead of a lock id:

lua
-- server
exports.np_passwords:RegisterVerifier('mdt', function(src, username, secret)
  local account = MDT.accounts[username:lower()]
  return account ~= nil and VerifyPasswordHash(secret, account.hash)
end)
lua
-- client
local ok = exports.np_passwords:Prompt({
  verify = 'mdt', type = 'login', label = 'MDT', sub = 'Mission Row PD', icon = 'laptop',
  username = lastUser,             -- optional prefill
})
if ok then TriggerServerEvent('mdt:open') end
lua
-- server
RegisterNetEvent('mdt:open', function()
  if not exports.np_passwords:ConsumeUnlock(source, 'verify:mdt') then return end
  -- …
end)
  • The function runs on the server in np_passwords' call; it gets the typed (or autofilled) username and secret and must return true. Errors count as a wrong try.
  • After a typed success the player gets Save this login?, so next time TAB fills it. The keychain entry is stored under verify:mdt.
  • There is no shared lockout: the prompt closes after Config.Prompt.verifyTries (5) wrong tries (reason tries).
  • Verifiers are dropped when your resource stops; register them again at start.
  • type may also be 'password', 'pin' (with length), 'phrase' or 'pattern'.

2FA from your resource ​

lua
-- server
exports.np_passwords:RegisterOtpApp({ id = 'mazebank', label = 'Maze Bank', icon = 'building-columns', tint = 'red' })

RegisterNetEvent('mazebank:transfer', function(to, amount)
  local src = source
  if amount >= 50000 and not exports.np_passwords:RequireCode(src, 'mazebank', { reason = 'Confirm transfer' }) then
    return
  end
  -- …
end)

See 2FA codes.

Cracking and police from your resource ​

lua
-- server: a heist with a faster, item-free laptop hack
local ok = exports.np_passwords:Crack(src, 'bank:vault', { speed = 2.0, item = false })

Crack yields until the minigame ends. Without opts, the player needs Config.Crack.item. See Crime & police.

Checklist ​

  • Register at every start; never store the code in your own database.
  • Open the prompt on the client or the server, but act only on ConsumeUnlock / IsUnlocked / the server Prompt result.
  • Prefix ids with your resource or place (apt12:, veh:, mdt:). stash: and gangstash: are reserved.
  • Don't spam Prompt: prompt.open is rate limited (4 per 2 s per player).