• Studio

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • API

    Services

    Edit

    Inventory

    The Inventory API provides functionality for handling owned items and currencies. Inventories can be stored per player, but also supports non-player ids to allow for features such as storage boxes for either a single player or group of players. Changes to the inventory are handled through InventoryTransactions which contains actions that are either all processed when a operation succeeds or are all skipped if the operation fails. This means you will never have cases such as a player buying an item from a shop within the world, losing their currency, but not getting the item, or cases such as two players trading items and the items end up duplicated or permanently lost. The Inventory API is only available on the server. See InventoryError for a list of errors that can be returned from Inventory functions. There is a limit of how often Inventory functions can be called before functions will start returning InventoryError.RequestThrottled. See the documentation for InventoryError.RequestThrottled for details.

    Methods

    ServerOnly

    Returns the data for a particular item owned by the holder. The callback parameters are an InventoryItem containing the item data and the error code. The item will be nil and the error code will be InventoryError.None if the holder does not own the item. The item will be nil and the error code will be a value other than InventoryError.None if an error occurred.

    Parameters

    holderId

    string

    itemId

    string

    callback

    function

    Returns

    void

    ServerOnly

    This is the equivalent of calling GetItem(player.user.id, callback).

      function PrintItem(player: Player, itemId: string)
        Inventory.GetPlayerItem(player, itemId, function(item)
          if(item == nil) then
            print(`Player {player.name} does not have {itemId}`)
          else
            print(`Player {player.name} has {item.amount} of {item.id}`)
          end
        end)
      end
    

    Parameters

    player
    Player
    itemId

    string

    callback

    function

    Returns

    void

    ServerOnly

    Returns up to the given limit number of items the holder owns. To get the first group of items, pass in nil for the cursorId. The callback includes a cursorId that can be passed to GetItems to retrieve the next group of items. If the cursor ID is nil, then there are no more items. The callback parameters are an array of InventoryItem, the cursor ID, and the error code. The array will be empty and the error code will be InventoryError.None if the holder owns no items. The array will be nil and the error code will be a value other than InventoryError.None if an error occurred.

      function GetItems(holder: string, limit: number, cursorId: string)
        Inventory.GetItems(holder, limit, cursorId, function(items, newCursorId, errorCode)
          if(items == nil) then
            print(`Got error {InventoryError[errorCode]} while getting items`)
            return
          end
                  
          for index, item in items do
            print(`{item.id} : {item.amount}`)
          end
          
          if(newCursorId ~= nil) then
            GetItems(holder, limit, newCursorId)
          else
            print("done")
          end
        end)
      end
      GetItems("SharedStorage", 15)
    

    Parameters

    holderId

    string

    limit

    number

    cursorId

    string

    callback

    function

    Returns

    void

    ServerOnly

    This is the equivalent of calling GetItems(player.user.id, limit, cursorId, callback).

    Parameters

    player
    Player
    limit

    number

    cursorId

    string

    callback

    function

    Returns

    void

    ServerOnly

    CommitTransaction will perform all the actions contained in the transaction if the returned error code is InventoryError.None or none of the actions if the returned error code is a value other than InventoryError.None. The callback parameters are the transaction ID and error code. The returned transaction ID can be used to call RollbackTransaction if the actions need to be undone.

    Parameters

    callback

    function

    Returns

    void

    ServerOnly

    Undos the actions of a committed transaction by passing in the transaction ID that was returned in the callback of CommitTransaction. The callback parameter is the error code.

    Parameters

    transactionId

    string

    callback

    function

    Returns

    void

    Updated 22 days ago

    PocketWorlds Icon

    © 2024 Pocket Worlds. All rights reserved.