How it works
- Items. A weapon is a non-stackable item in
config/items.lua, mapped to a game weapon inconfig/weapons.lua. Its state lives in the item’s metadata, so it moves with the item: give it, stash it, drop it. - Serials. Every new weapon item gets a unique serial, recorded with the character it was created for. See GetBySerial.
- Equipping. Using a weapon item equips it. Using it again puts it away. The server calls the weapon natives, so a client cannot equip what it does not own.
- Ammo. Ammo is an item. Reload moves ammo items into the weapon, up to its
capacity. Using an ammo item reloads the equipped weapon. - Shots and wear. The client reports its ammo count as it shoots. The server only accepts the count going down. Each shot wears the weapon by
wearout of 100 durability. At 0 it breaks: it is put away and cannot be equipped until repaired. - Attachments and tints. Attachment and tint items are used up into the weapon’s metadata. Components come back off, tints do not. Using an attachment fits it on the equipped weapon.
- Out of hands. A weapon is put away when its item leaves the player’s inventory, and when the player goes down, dies or unloads the character.
Two natives have no server version: reading ammo and setting tints. The core client does both and reports to the server, which checks every report. See Security.
Config
config/weapons.lua
Every item named here MUST exist in
config/items.lua, and weapon items MUST have stack = false. Otherwise boot stops with the item name.
The default config comes with weapon_pistol, ammo_9mm, pistol_suppressor and weapon_tint_gold, which config/items.lua also defines by default.
Metadata
New weapons get
ammo = 0, durability = 100 and a serial. Pass metadata to AddItem to start from other values, such as a loaded weapon for a shop. Ammo and durability updates do not write audit rows. Attaching, detaching and repairing do.
Server
Equip / Unequip
invalid_args. Downed and dead players get no_permission. Weapons:BeforeEquip MAY cancel.
GetEquipped
Reload
insufficient_items without ammo, not_found without an equipped weapon.
Unload
cannot_carry when they do not fit.
Attach / Detach
Attach uses one attachment or tint item from itemSlot on the weapon in weaponSlot. Items that do not fit that weapon, or are already fitted, return invalid_args. Detach gives a component back, by item name.
Repair
target is a player source or an inventory id, so a gunsmith can repair weapons in a stash.
GetBySerial
record is { serial, item, characterId?, createdAt }, or nil for unknown serials. characterId is the character the weapon was created for, such as the buyer.
IsWeapon / GetDefinition
Client
ok, errorCode:
ug-core:Weapons:Equipped with (hash, ammo) and ug-core:Weapons:Unequipped let a HUD show the weapon.
client.lua
Security
Events
reason is toggle, switch, moved, removed, down, unload, broken or request.
Hooks
It allows or cancels. A safe zone can refuse every equip: