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:
- Get the QBCore object: This is necessary to access QBCore functions and data.luaCopy code
local QBCore = exports['qb-core']:GetCoreObject()
- 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)
- Add the item: Use
Player.Functions.AddItem
to add the item to the player’s inventory.luaCopy codePlayer.Functions.AddItem(itemName, amount)
- Notify the player: Optionally, you can notify the player that they received the item.luaCopy code
TriggerClientEvent('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.