Skip to Content

Inventory

The inventory providers provide a service that allows resources to interact with the inventory of players.

Usage

To retrieve the inventory provider, use the load function with "inventory" for the service parameter.

Lua:

local inventoryProvider = exports.r3_servicesmanager:load("inventory")

JavaScript:

const inventoryProvider = exports.r3_servicesmanager.load("inventory");

Server

On the server side, the following methods are available:

addItem

Adds an item to the specified player’s inventory.

inventoryProvider.addItem(playerId, itemName, amount)

Parameters:

  • playerId: integer
  • itemName: string
  • amount: integer

Returns:

  • true if the item was successfully added.
  • false if the item wasn’t successfully added (i.e. the player could not carry it).

removeItem

Removes an item from the specified player’s inventory.

inventoryProvider.removeItem(playerId, itemName, amount)

Parameters:

  • playerId: integer
  • itemName: string
  • amount: integer

Returns:

  • true if the item was successfully removed.
  • false if the item wasn’t successfully removed (i.e. the player did not have the specified amount).

getItemCount

Returns the count of an item in the specified player’s inventory.

inventoryProvider.getItemCount(playerId, itemName)

Parameters:

  • playerId: integer
  • itemName: string

Returns:

  • integer

hasItem

Checks whether a player has a certain item and if the amount of the given item is bigger than or equal to the specified amount.

inventoryProvider.hasItem(playerId, itemName, amount)

Parameters:

  • playerId: integer
  • itemName: string
  • amount: integer

Returns:

  • true if the player has the given item and the amount of the given item is bigger than or equal to the specified amount.
  • false if the the player doesn’t have the given item or the amount of the given item is less than the specified amount.

Registration

To register an inventory provider, ensure it implements all methods described above. Check out inventory.lua and inventory.ts for type definitions.

When your provider object implements all methods, you can register it as follows:

Lua:

exports.r3_servicesmanager:register("inventory", provider, priority, GetCurrentResourceName())

JavaScript:

exports.r3_servicesmanager.register("inventory", provider, priority, GetCurrentResourceName());

For priority, it is recommended to use priority 2, this corresponds to ServicePriority.Normal. It would be even better to allow server owners to configure the used priority in the configuration of your resource so that they can choose which resource takes priority.