ug-inventory shows the items and calls the client functions below. Owners can swap the UI without weakening security.
This page covers player inventories. Stashes, vehicle inventories and ground drops come next.
Concepts
- Slots and weight. Each inventory has a number of slots and a maximum weight in grams. Each slot holds one stack. Player inventories use
playerSlotsandplayerWeightfromconfig/inventory.lua. - All or nothing. Every change either applies fully or not at all. Adding 12 water that only fits 10 adds none and returns
cannot_carry. - Stacking. Units stack up to the item’s
maxStack, only with the same name and equal metadata. Existing stacks fill first, then empty slots, lowest slot first. - Targets.
targetis a player source, for that player’s inventory, or an inventory id. Player inventories areplayer:<characterId>and exist while the character is loaded. - Audit log. Every change writes a row to
ug_item_logs, in the same database transaction as the inventory. See GetLog.
Adding and removing
AddItem
integer
required
1 to 1,000,000.
table
Checked by the item’s rule.
string
Up to 128 characters, stored in the audit log.
RemoveItem
metadata, only stacks whose metadata has those keys and values count.
RemoveFromSlot
count defaults to the whole stack.
SetMetadata
0.
Clear
Moving
Between two inventories, the target’s weight is checked, and both sides get an audit row pointing to the other.
Reading
table
A copy.
GetSource returns the player who owns an inventory id, or nil.
Use
- The slot has an item with a use handler. Otherwise
not_foundorinvalid_args. - The player is alive, unless the handler allows otherwise. Otherwise
no_permission. - The item’s cooldown passed. Otherwise
rate_limited. Items:BeforeUseallows it. Otherwiseno_permission.- The handler runs. Returning
falserefuses the use withno_permission. An error returnsinternal_error. consumeunits leave the slot, andug-core:Items:Usedfires.
GetLog
integer
default:"25"
1 to 100.
UgItemLogEntry[]
Client
The player’s own inventory, sent by the server to that player only. Reads are local:ok, errorCode:
Give needs the other player within giveDistance meters, measured on the server.
Build an inventory UI
client.lua
ug-core:Inventory:Changed is a client event: nil after a full sync, or the list of changed slots.
Security
Clients send slots and counts, never items, metadata or inventory ids. Their own inventory is the referenceplayer.
Each replies
true or an error code. The client functions above call them for you.
Errors
Wrong argument types, counts out of range and long reasons raise.
Events
Use
GetSource(inventoryId) to find the player of a player inventory.
Hooks
They allow or cancel. Changes to the payload are ignored.
Death
Items stay when a player dies. WithclearOnRespawn = true in config/inventory.lua, respawning empties the inventory, except items with keepOnRespawn. Inventory:BeforeClearOnRespawn can cancel it, for example in an arena.
Player object
Player objects have shortcuts:player:AddItem, RemoveItem, GetItemCount, HasItem, CanCarry, GetItems, UseItem.