Handles all the player's storage such as personal, vehicle, stash, drops
qb-shops integration for displaying all items available to buy
Built-in support for usable vending machines
All exports listed are SERVER only unless specified
Inventory State
The inventory state is controlled via state bags using a state bag name of inv_busy. You can use this state to control whether the inventory should be able to be opened or not
Example (server):
RegisterCommand('checkState', function(source)local player =Player(source)local state = player.state.inv_busyprint('Inventory current state is', state)end, true)RegisterCommand('lockInventory', function(source)local player =Player(source) player.state.inv_busy =trueprint('Inventory current state is', state)end, true)RegisterCommand('unlockInventory', function(source)local player =Player(source) player.state.inv_busy =falseprint('Inventory current state is', state)end, true)
Items support additional information that can be added to them via an info attribute. This information will display on the item when the player hovers over it in a key,value pair format
RegisterCommand('closeInventory', function(source) exports['qb-inventory']:CloseInventory(source)print('Inventory closed for player '..source)end, true)RegisterCommand('closeInventoryByName', function(source,identifier) exports['qb-inventory']:CloseInventory(source, identifier)print('Inventory closed for player '..source..' and inventory '..identifier..' set to closed')end, true)
RegisterCommand('setItemData', function(source,args)local itemName = args[1]local key = args[2]local val = args[3]ifnot itemName ornot key ornot val thenreturnendlocal success = exports['qb-inventory']:SetItemData(source, itemName, key, val)if success thenprint('Set data for item '..itemName..': '..key..' = '..val)elseprint('Failed to set data for item '..itemName)endend, true)
Item Info Example:
RegisterCommand('setItemData', function(source)local itemName ='markedbills'local key ='info'local val = { worth =1000 }ifnot itemName ornot key ornot val thenreturnendlocal success = exports['qb-inventory']:SetItemData(source, itemName, key, val)if success thenprint('Set data for item '..itemName..': '..key..' = '..json.encode(val, { indent =true }))elseprint('Failed to set data for item '..itemName)endend, true)
UseItem
exports['qb-inventory']:UseItem(itemName, ...)
itemName: string
. . . : function
Example:
RegisterCommand('useItem', function(source,args)local itemName = args[1]ifnot itemName thenreturnend exports['qb-inventory']:Useitem(itemName, function()print('Used item with the name of '..itemName)end)end, true)
HasItem
This export is also available to use on the client
RegisterCommand('hasSingleItem', function(source)local item ='item1'local amount =5local hasItem = exports['qb-inventory']:HasItem(source, item, amount)if hasItem thenprint('Player '..source..' has '..amount..' of '..item)elseprint('Player '..source..' does not have '..amount..' of '..item)endend, true)RegisterCommand('hasMultipleItems', function(source)local items = {'item1', 'item2'}local amount =5local hasItems = exports['qb-inventory']:HasItem(source, items, amount)if hasItems thenprint('Player '..source..' has '..amount..' of each item: '..table.concat(items, ', '))elseprint('Player '..source..' does not have '..amount..' of each item: '..table.concat(items, ', '))endend, true)RegisterCommand('hasMultipleItemsWithAmounts', function(source)local itemsWithAmounts = {item1 =5, item2 =10}local hasItemsWithAmounts = exports['qb-inventory']:HasItem(source, itemsWithAmounts)if hasItemsWithAmounts thenprint('Player '..source..' has the specified items with their amounts')elseprint('Player '..source..' does not have the specified items with their amounts')endend, true)
RegisterCommand('getItemByName', function(source,args)local itemName = args[1]ifnot itemName thenreturnendlocal item = exports['qb-inventory']:GetItemByName(source, itemName)if item thenprint('First occurrence of item '..itemName..' is in slot: '..item.slot)elseprint('No item found with name '..itemName)endend, true)
RegisterCommand('getItemsByName', function(source,args)local itemName = args[1]ifnot itemName thenreturnendlocal items = exports['qb-inventory']:GetItemsByName(source, itemName)if#items >0thenprint('Items named '..itemName..' found in slots:')for _, item inipairs(items) doprint(item.slot)endelseprint('No items found with name '..itemName)endend, true)
RegisterCommand('getItemCount', function(source,args)local itemName = args[1]ifnot itemName thenreturnendlocal itemCount = exports['qb-inventory']:GetItemCount(source, itemName)if itemCount and itemCount >0thenprint('You have '..itemCount..' of item: '..itemName)elseprint('No items found with name '..itemName)endend, true)RegisterCommand('getItemCounts', function(source)local itemNames = {"apple", "banana", "orange"}local itemCount = exports['qb-inventory']:GetItemCount(source, itemNames)if itemCount and itemCount >0thenprint('You have '..itemCount..' of the items: '..table.concat(itemNames, ", "))elseprint('No items found with the names: '..table.concat(itemNames, ", "))endend, true)