Items & inventory API
All of this is server side on bs_inventory. The full list with file and line is on the bs_inventory API page.
Defining an item
Section titled “Defining an item”Add it to Items.Definitions in bs_inventory/config/config_items.lua:
radio_part = { label = 'Radio part', category = 'material', weight = 0.3, w = 1, h = 1, stack = 10, usable = false, value = 20 },| Field | |
|---|---|
label | shown name |
category | medical, food, drink, weapon, ammo, material, tool, booster, gadget, drug, gear… drives the F10 tabs and some behaviour |
weight, w, h | kg and grid size |
stack | max per slot |
usable | can be used from the inventory |
value | base price for traders |
data | behaviour: heal, hunger, thirst, armour, weapon, ammo, drug, handled, defaultMeta, craftingStation… |
Definitions are synced into bs_item_definitions on start.
Give, take, count
Section titled “Give, take, count”local inv = exports.bs_inventory
inv:GiveItem(src, 'bandage', 2) -- metadata optionalinv:GiveItem(src, 'booster_drone', 1, { battery = 50, capacity = 'medium' })inv:RemoveItem(src, 'bandage', 1)local n = inv:CountItem(src, 'bandage')
local def = inv:GetItem('bandage') -- runtime definitionlocal list = inv:ItemMetaList(src, 'booster_drone') -- metadata of every stackinv:SetItemMeta(src, slot, 'booster_drone', meta, true)inv:Refresh(src) -- push state to the clientCheck the return value of GiveItem. A full bag is a real case.
Making an item do something when used
Section titled “Making an item do something when used”Two ways:
- Use the built-in
datafields (heal, hunger…) and the inventory does it. - Handle it yourself. Set
data.handled = trueso the inventory doesn’t consume it, then listen to the use hook and remove it yourself when it worked:
-- server, in your resourceAddEventHandler('bsesx:useItem:radio_part', function(src) if not doTheThing(src) then return end exports.bs_inventory:RemoveItem(src, 'radio_part', 1)end)The bsesx:useItem:<item> event fires for every used item after the built-in effects. It’s named for the ESX bridge but it’s a plain server event, any resource can listen.
If you also report big item gains to the anti-cheat: exports.bs_guard:ReportItem(src, item, amount).
Containers
Section titled “Containers”Anything with slots that isn’t the player bag: crates, car boots, stashes, event drops.
inv:RegisterContainer('myres:crate:1', { width = 6, height = 4, maxWeight = 150.0, label = 'Crate' })-- optional 3rd arg: a src that is allowed to open it right awayinv:ContainerGive('myres:crate:1', 'scrap_metal', 5)inv:OpenContainerFor(src, 'myres:crate:1')local has = inv:ContainerHasItems('myres:crate:1')local items = inv:ContainerContents('myres:crate:1')Register it on the server after your own checks (distance, lock, ownership). The inventory only trusts this table for size, never what a client says. Ids are max 64 chars, prefix them with your resource. bs_events/server/sv_main.lua is a clean example.
Loadouts (hotbar, boosters, gadgets)
Section titled “Loadouts (hotbar, boosters, gadgets)”inv:LoadoutGet(src, 'gadget') -- includes metadatalocal ok, meta = inv:LoadoutTake(src, 'gadget', 1, 1, 'booster_drone')inv:LoadoutPut(src, 'gadget', 1, 'booster_drone', meta)Weapons and ammo
Section titled “Weapons and ammo”ItemForWeapon(model) gives the item for a WEAPON_X, AmmoItemFor(weaponName) the ammo item.
Wiping
Section titled “Wiping”WipeInventory(src) empties a player. Superadmin only from the F10 menu, use it with care from code.
bs19.org · Privacy · Terms · Cookie settings
Cookies and outside content
The site only needs two essential cookies to keep you logged in and protect forms. Videos and streams from YouTube and Twitch load from their servers, which can set their own cookies, so they stay off until you say yes. Nothing is pre-ticked and you can change your mind any time with "Cookie settings" at the bottom of every page. Details.
Questions about the server, the framework or the site? A real person reads this, usually within a day.