🎒InventoryService

Service for handling inventory actions.

Example usage:

-- Set up a 20% chance to drop an emerald when damaging enemies
Events.EntityDamage(function(event)
    if not event.fromEntity then
        return
    end
    
    -- Only give emeralds when random between 1 and 5 is 1
    if math.random(1, 5) ~= 1 then
        return
    end
    
    local fromPlayer = event.fromEntity:getPlayer()
    if fromPlayer then
        InventoryService.giveItem(fromPlayer, ItemType.EMERALD, 1, true)
    end
end)

Functions

giveItem(player: Player, itemType: ItemType, amount: number, playWorldEffect: bool)

Gives a player a specified number of an item. If playWorldEffect is true, an effect will play showing the recipient getting their items.

getAmount(player: Player, itemType: ItemType): number

Returns amount of specified item the player has (0 if they have none).

removeItemAmount(player: Player, itemType: ItemType, amount: number)

Removes the amount of the item from the player's inventory.

clearInventory(player: Player)

Clears all items from the player's inventory.

getInventory(player: Player): { itemType: ItemType, amount: number }[]

Returns a table of the amount of each item the player has in their inventory - including armor and hand items.

Example:

local player = PlayerService.getPlayerByDisplayName("quinticat")
local inv = InventoryService.getInventory(player)
for i, data in ipairs(inv) do
    print(player.name .. " has " .. data.amount .. " " .. data.itemType)
end

Last updated