Integrating your resource ​
Three patterns, with one worked example each. The full reference is Exports & results; every game's params are on the games pages.
| Your flow | Use |
|---|---|
| The server decides when a player plays (a door, a vault, a job step the server drives) | Play from the server, optionally with opts.item |
| Several players solve together | Team |
| The client runs the flow and tells the server the outcome (crafting, digging, prison jobs) | a profile + receipt |
Client exports such as exports.np_minigames:Play('skillcheck') run on the client and can be faked. Only use them for
cosmetic or low-stakes gates. Never trust a "success" the client sends.
Ground rules ​
-
np_minigames is optional. Detect it at call time and keep your old minigame as the fallback:
lualocal function minigames() return Config.Minigames.enabled and GetResourceState('np_minigames') == 'started' end -
Put the game, difficulty and opts in your config (
Config.Minigames), so server owners can choose. Every np_* resource does this; see Integrations. -
Keep your own checks (tokens, distance, timing) around the call. np_minigames adds its own on top.
-
Play,TeamandChainblock the calling thread. Call them from a thread, for example inside a net event handler (which already runs in one).
1. Server Play with an item ​
A lockpick that is needed for the attempt, breaks one time in three on a fail, and wears down by 10 points each try.
-- config.lua
Config.Minigames = {
enabled = true,
lockpick = {
game = 'lockpick', difficulty = 'medium',
item = { name = 'lockpick', remove = 'fail', chance = 0.35, degrade = 10 },
},
}
-- server.lua
RegisterNetEvent('myres:pickDoor', function(doorId)
local src = source
local door = Doors[doorId]
if not door or #(GetEntityCoords(GetPlayerPed(src)) - door.coords) > 2.0 then return end
if not minigames() then return LegacyLockpick(src, door) end
local m = Config.Minigames.lockpick
local ok, result = exports.np_minigames:Play(src, m.game, {
difficulty = m.difficulty, item = m.item,
label = door.label, icon = 'key', maxMove = 2.0, coords = door.coords,
})
if result.reason == 'no_item' then
return Notify(src, 'You need a lockpick.')
end
if result.item and result.item.removed then
Notify(src, 'Your lockpick broke.')
end
if ok then UnlockDoor(doorId) end
end)
okisresult.success. The server replayed the player's input, so you can trust it.- Refusals (
no_item,busy,disabled, …) returnfalsebefore anything is shown, so checkresult.reason. - Nothing is used up when the player drops or your resource stops mid-game (
dropped,api). - Want a progress bar elsewhere, for example on an ATM screen? Add
onProgress = function(p) ... end(0..100, cosmetic only).
2. Team ​
Two players hack two terminals at once and must finish within 6 seconds of each other. Each one needs a laptop, which is used up whatever happens.
-- server.lua
local function startSyncHack(a, b)
if not minigames() then return LegacySyncHack(a, b) end
local ok, team = exports.np_minigames:Team({ a, b }, 'hexhack', {
difficulty = 'hard', window = 6, label = 'Pacific terminal',
item = { name = 'laptop', remove = 'always' },
})
if not ok then
-- 'busy' / 'no_item' (refused before anyone started), 'out_of_sync', 'team_failed', or a member's own reason
print(('sync hack failed: %s'):format(team.reason))
return false
end
OpenVault()
return true
end
team.results[src]holds each member's own result.team.spreadMsis the gap between the first and the last finish.- Every member must have the item, or the round is refused before anyone starts.
TeamAsync(srcs, id, opts, function(team) end)is the non-blocking version.- While a team game runs, every member sees a status strip with the others' progress.
3. Profile + receipt ​
A digging job runs on the client: the player walks to a spot, plays a skill check, and the client tells the server.
With a profile, the server decides the game, and the client hands in a receipt instead of success.
-- server.lua: register once, when the resource starts (np_minigames may start after you)
local PROFILE = 'dig'
local function registerProfiles()
if GetResourceState('np_minigames') ~= 'started' then return end
exports.np_minigames:RegisterProfile(PROFILE, {
game = 'skillcheck', levels = { 'easy', 'medium', 'medium' },
label = 'Dig', icon = 'person-digging', cancelable = true,
item = { name = 'shovel', degrade = 5 },
cooldownMs = 5000,
}) -- -> 'myres:dig'
end
AddEventHandler('onResourceStart', function(res)
if res == GetCurrentResourceName() or res == 'np_minigames' then registerProfiles() end
end)
-- client.lua
local function dig(spot)
if GetResourceState('np_minigames') ~= 'started' then return LegacyDig(spot) end
local ok, result = exports.np_minigames:PlayProfile('myres:dig', { sub = spot.label })
-- send the receipt, whatever the outcome: the server decides
if result.receipt then TriggerServerEvent('myres:dig:finish', spot.id, result.receipt) end
end
-- server.lua
RegisterNetEvent('myres:dig:finish', function(spotId, receipt)
local src = source
if not ValidSpotFor(src, spotId) then return end -- your own checks stay
local result, err = exports.np_minigames:Redeem(src, receipt, PROFILE)
if not result then
return print(('refused dig receipt from %d: %s'):format(src, err)) -- forged, reused, expired, wrong player
end
if result.success then GiveDigLoot(src, spotId, result.grade) end
end)
- The client can only add
label,subandtitle. The game, difficulty, params and item come from the profile. Redeemworks once, only for the player who played, within 120 s (Config.Security.receiptTtlMs).- A failed run also has a receipt (
result.success == false), so you can punish a fail on the server too. - Your profiles are removed when your resource stops, and register again when it starts. If np_minigames restarts,
register them again too (the
onResourceStarthandler above does both).
Tests ​
Stub np_minigames in your harness and test both paths:
H.resources.np_minigames = 'started'
H.exports.np_minigames = {
Play = function(src, id, opts) return true, { success = true, game = id, item = { name = 'lockpick', removed = false } } end,
Redeem = function(src, receipt, profile)
if receipt ~= 'r-good' then return nil, 'no_receipt' end
return { success = true, profile = 'myres:dig', receipt = receipt }
end,
RegisterProfile = function(name) return 'myres:' .. name end,
}
Cover the fallback with np_minigames stopped, the server refusing a forged success, and the server refusing a forged
or reused receipt.