OnPlayerInventoryChange
What the player carries or their purse changed - the moment to save it.
Their game reported a different inventory from the last one (within about a second of the change - an item picked up,
dropped, put in a chest, bought, eaten, or a Groschen spent) and the inventory audit has judged
it. 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. It does not run for their horse’s saddlebags (OnHorseItemsChange).
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
