How to add item on qbcore (qbcore.functions.additem)

You can qbcore.functions.additem on qbcore 2 ways .

To add an item in QBCore, you can use the QBCore.Functions.AddItem function. Here’s a basic example of how to use it in a script:

local QBCore = exports['qb-core']:GetCoreObject()

-- Example of giving an item to a player
local src = source -- this is the player's server ID
local Player = QBCore.Functions.GetPlayer(src)

-- Add item to player's inventory
local itemName = 'bread' -- replace with the item you want to add
local amount = 1 -- replace with the amount of items you want to add

Player.Functions.AddItem(itemName, amount)

-- Notify the player
TriggerClientEvent('QBCore:Notify', src, 'You received '..amount..' '..itemName..'(s)', 'success')

Explanation:

  1. Get the QBCore object: This is necessary to access QBCore functions and data.luaCopy codelocal QBCore = exports['qb-core']:GetCoreObject()
  2. Get the player object: You need to get the player object using the player’s server ID (source).luaCopy codelocal src = source local Player = QBCore.Functions.GetPlayer(src)
  3. Add the item: Use Player.Functions.AddItem to add the item to the player’s inventory.luaCopy codePlayer.Functions.AddItem(itemName, amount)
  4. Notify the player: Optionally, you can notify the player that they received the item.luaCopy codeTriggerClientEvent('QBCore:Notify', src, 'You received '..amount..' '..itemName..'(s)', 'success')

Make sure that the item you are adding exists in your qb-core/shared/items.lua file. If you need to add new items, you can define them in that file.

The function QBCore.Functions.AddItem is used in QBCore, a popular framework for creating FiveM servers, to add an item to a player’s inventory. The function typically requires parameters like the player’s ID, the item name, the amount, and sometimes metadata.

Here’s an example of how it might be used:

-- Add an item to the player's inventory
local playerId = source -- The player's ID (can be obtained from an event or function)
local itemName = "lockpick" -- The name of the item you want to add
local amount = 1 -- The amount of the item you want to add
local metadata = {} -- Optional metadata (e.g., for unique items)

-- Add the item to the player's inventory
QBCore.Functions.AddItem(playerId, itemName, amount, metadata)

In this example, a lockpick is added to the player’s inventory. If you need to add more complex or specific items, you might include metadata or additional parameters.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart