Inventory:getItemCount

From Nutscript Developer Wiki


   Inventory:getItemCount(uniqueID, onlyMain)

This function returns the number of items that existing in the inventory. This function returns integer.

Arguments[edit]

  • String uniqueID
  • Boolean onlySearchMain

Return Values[edit]

  • Integer

A Number of the items

Examples[edit]

If player has a skull in their inventory, it will print how many they have.

    for k,v in pairs(player.GetAll()) do
         print(v:getChar():getInv():getItemCount( "skull" ))
    end

Can be used to check if a player has a certain amount of items in a table. This will return false if the player does not have the item in their inventory, has less items in their inventory than defined in the table or returns true if they have the exact items needed or more.

    local items = {
    ["skull"] = 5,
    ["bone"] = 3,
    }
    for k,v in pairs( items ) do
         if player:getChar():getInv():hasItem( k ) then
              if player:getChar():getInv():getItemCount( k ) < v then
                   return false
              end
         end
         if !player:getChar():getInv():hasItem( k ) then
              return false
         end
    end
    return true