Add an item#
Items are defined in rsg-core's shared data and stored by rsg-inventory. This guide covers defining an item, adding its image, giving and removing it, and making it usable.
1. Define the item#
An item has these fields: name, label, weight, type ('item' or 'weapon'), image, unique, useable, shouldClose, description and category. The category is used for filtering in the inventory.
You can register one from any server resource at runtime. rsg-core validates it, syncs it to every client and replays it to players who join later:
local ok, msg = exports['rsg-core']:AddItem('treasure_map', {
name = 'treasure_map',
label = 'Treasure Map',
weight = 50,
type = 'item',
image = 'treasure_map.png',
unique = true,
useable = true,
shouldClose = true,
description = 'An old map',
category = 'misc',
})
if not ok then print('AddItem failed:', msg) end -- e.g. 'treasure_map_exists'
AddItems({ [name] = data, ... }) registers many items at once and validates all of them first, so a failure never leaves half a batch applied. UpdateItem(name, data) and RemoveItem(name) work the same way.
To make the item permanent in the framework itself, add it to rsg-core's shared/items.lua instead (see "Shared Data" in the rsg-core documentation).
2. Add the image#
Put a PNG in rsg-inventory/html/images/ and set image to its file name. The inventory reads images from nui://rsg-inventory/html/images/ by default (Config.ImagePath). If an item shows no picture, the file is missing or the image field does not match.
3. Give and remove it#
Use the rsg-inventory server exports. Check there is room first:
local RSGCore = exports['rsg-core']:GetCoreObject()
local function giveMap(src)
if not exports['rsg-inventory']:CanAddItem(src, 'treasure_map', 1) then
return RSGCore.Functions.Notify(src, 'You cannot carry that.', 'error')
end
exports['rsg-inventory']:AddItem(src, 'treasure_map', 1, { area = 'valentine' }, nil, 'quest-reward')
end
The arguments are AddItem(target, name, amount, info?, slot?, reason?). The item box notification and the log entry are handled for you. RemoveItem(target, name, amount, slot?, reason?) is the matching call, and HasItem(target, items, amount) and GetItemCount read the inventory.
Do not add money items such as dollar or cent yourself when money items are enabled. Use Player.Functions.AddMoney so player data, inventory and HUD stay in sync.
4. Make it usable#
Register a handler on the server and mark the item useable = true:
RSGCore.Functions.CreateUseableItem('treasure_map', function(source, item)
local Player = RSGCore.Functions.GetPlayer(source)
if not Player then return end
-- item is the slot data: name, amount, info, slot
TriggerClientEvent('rsg-hello:client:openMap', source, item.info)
end)
Validate everything on the server. The client only gets told what to show.
Troubleshooting#
| Problem | Likely cause |
|---|---|
| Item has no image | PNG missing from html/images/, or image does not match the file name |
AddItem returns <name>_exists | An item with that name is already registered; use UpdateItem |
| Using the item does nothing | CreateUseableItem is missing, or the item is not marked useable |
| Item cannot be added | No weight or slots left; check with CanAddItem first |