OnPlayerInventoryChange
What the player carries or their purse changed - the moment to save it.
What the server’s records hold of the player’s bag or purse changed: an item picked up, dropped, put in a chest, bought,
eaten or sold, a Groschen spent, what they wear, an item the mode gave or took (GivePlayerItem, SetPlayerInventory -
the gives and takes still on their way count, so this runs when the mode orders the change, not when the player’s game has
done it), an item’s condition or quality (a sword forged at the anvil comes with its quality a moment after it is made), a
tagged item’s name, text, weight or price (SetPlayerItemTag, SetPlayerItemWeight,
SetPlayerItemPrice) - at most once a second for one player. Read the new state with GetPlayerInventory and GetPlayerMoney. A mode
that keeps what players carry saves here: what a player hands to a chest, the ground or a shop is the server’s at once, so
saving on a timer alone leaves a window in which a crash gives the item back twice. Their horse’s saddlebags are
OnHorseItemsChange’s.
Syntax
Section titled “Syntax”function OnPlayerInventoryChange(pid) -- ...end| Parameter | Type | |
|---|---|---|
pid |
number | the player |
Returns
Section titled “Returns”nothing
Example
Section titled “Example”-- the bags saved on every change, as one row per stack (db: GetDatabase(); the player's row id in their data)function OnPlayerInventoryChange(pid) local id = GetPlayerData(pid, "db_id") if not (db and id) then return end local batch = { { "DELETE FROM player_items WHERE player_id = @p", { p = id } } } for slot, it in ipairs(GetPlayerInventory(pid)) do batch[#batch + 1] = { "INSERT INTO player_items (player_id, slot, item, amount) VALUES (@p, @s, @i, @a)", { p = id, s = slot, i = it.class, a = it.amount } } end db:Batch(batch, function(ok, err) if not ok then Log("bags not saved: " .. tostring(err)) end end)endSee also
Section titled “See also”GetPlayerInventory · SetPlayerInventory · OnHorseItemsChange · GetDatabase · the Items and inventory group of the index
