Combining items to get X amount in return

We cannot get this, looking to the hivemind for help.

We are simply trying to convert a brick of coke into multiple bags of coke.

RegisterNetEvent(“QBCore:Client:OnPlayerLoaded”)
while QBCore == nil do
QBCore.Functions.CreateUseableItem (‘razor_blade’, function(source)
local xPlayer = QBCore.Functions.GetPlayerData()
if xPlayer.Functions.GetItemByName(‘kilo_coke’).count > 0 then
xPlayer.Functions.RemoveItem(‘kilo_coke’, 1)
Citizen.Wait(0)
xPlayer.Functions.AddItem(‘cokebaggy’, 10)
elseif xPlayer.Functions.GetItemByName(‘kilo_coke’).count > 0 then
xPlayer.Functions.RemoveItem(‘kilo_coke’, 1)
Citizen.Wait(0)
xPlayer.Functions.AddItem(‘cokebaggy’, 10)
end
end)

Any help would be hugely appreciated!

Place the following code into your resources server-side file.

QBCore.Functions.CreateUseableItem("razor_blade", function(source, item)
	local source = source
	local xPlayer = QBCore.Functions.GetPlayer(source)
	
	if not xPlayer.Functions.GetItemByName(item.name) then return end
	if xPlayer.Functions.GetItemByName("kilo_coke").count > 0 then
		xPlayer.Functions.RemoveItem("kilo_coke", 1)
		Wait(0)
		xPlayer.Functions.AddItem("cokebaggy", 10)
	end
end)

I put that in there and got no errors yet nothing seems to be happening. Do the items have to be “unique”?

Can you share your code?

And have you made sure your items (“razor_blade”, “kilo_coke” and “cokebaggy”) are entered into the inventory system you’re using? You need to make sure the item(s) are in the respective item files. If you’re using either inventory systems below, here are the locations to the item files.

qb-inventory:
qb-core\shared\items.lua

ox_inventory
ox_inventory\data\items.lua

Yeah they are in there. I have them on my character right now.

	['cokebaggy']  = {['name'] = 'cokebaggy', 	['label'] = 'Bag of Coke', ['weight'] = 0, 	['type'] = 'item', 	['image'] = 'cocaine_baggy.png', 	['unique'] = false, ['useable'] = true, ['shouldClose'] = true,   ['combinable'] = nil,   ['description'] = 'To get happy real quick'},
	['kilo_coke']  = {['name'] = 'kilo_coke',   ['label'] = 'A Kilo of Coke', ['weight'] = 1000, ['type'] = 'item', ['image'] = 'coke_brick.png', 	['unique'] = true, 	['useable'] = true, ['shouldClose'] = true,	  ['combinable'] = nil,   ['description'] = 'Heavy package of cocaine, mostly used for deals and takes a lot of space'},
	['razor_blade'] = {['name'] = 'razor_blade', ['label'] = 'A razor blade', ['weight'] = 10000, ['type'] = 'item', ['image'] = 'razor_blade.png', ['unique'] = true, 	['useable'] = true, ['shouldClose'] = false,  ['combinable'] = nil,   ['description'] = 'A handy tool for stuff'},

Restart your server after making these edits suggested by @LinxNZ
As far as I was aware using any QB CORE Server Functions require a server restart to load properly from experience. QB Server Functions Document

Assuming you have restarted your server (like @haws1290 suggested) and it still doesn’t work, can you share your resources server-side code?

server restart did not seem to have any affect.

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

TriggerEvent('QBCore:GetObject', function(obj) QBCore = obj end)

RegisterServerEvent('qb-coke:pickedUpCoke') --hero
AddEventHandler('qb-coke:pickedUpCoke', function()
	local src = source
	local Player = QBCore.Functions.GetPlayer(src)

	    if 	TriggerClientEvent("QBCore:Notify", src, "You Got Some Coke Leaves.", "Success", 8000) then
		  Player.Functions.AddItem('cocaine_leaf', 1) ---- change this shit 
		  TriggerClientEvent("inventory:client:ItemBox", source, QBCore.Shared.Items['cocaine_leaf'], "add")
	    end
end)	

RegisterServerEvent('qb-coke:processcoke')
AddEventHandler('qb-coke:processcoke', function()
	local src = source
	local Player = QBCore.Functions.GetPlayer(src)

	if Player.Functions.GetItemByName('cocaine_leaf') and Player.Functions.GetItemByName('empty_weed_bag') then
		local chance = math.random(1, 8)
		if chance == 1 or chance == 2 or chance == 3 or chance == 4 or chance == 5 or chance == 6 or chance == 7 or chance == 8 then
			Player.Functions.RemoveItem('empty_weed_bag', 1)----change this
			Player.Functions.RemoveItem('cocaine_leaf', 2)---change this
			Player.Functions.AddItem('cocaine_bag', 1)----change this
			TriggerClientEvent("inventory:client:ItemBox", source, QBCore.Shared.Items['cocaine_leaf'], "remove")
			TriggerClientEvent("inventory:client:ItemBox", source, QBCore.Shared.Items['empty_weed_bag'], "remove")
			TriggerClientEvent("inventory:client:ItemBox", source, QBCore.Shared.Items['cocaine_bag'], "add")
			TriggerClientEvent('QBCore:Notify', src, 'Coke Processed successfully', "success")  
		end 
	else
		TriggerClientEvent('QBCore:Notify', src, 'You don\'t have the right items', "error") 
	end
end)

--selldrug ok

RegisterServerEvent('qb-coke:selld2')
AddEventHandler('qb-coke:selld2', function()
	local src = source
	local Player = QBCore.Functions.GetPlayer(src)
	local Item = Player.Functions.GetItemByName('cocaine_bag')
   
	
      
	for i = 1, Item.amount do
	if Item.amount >0 then
	if Player.Functions.GetItemByName('cocaine_bag') then
		local chance2 = math.random(1, 8)
		if chance2 == 1 or chance2 == 2 or chance2 == 9 or chance2 == 4 or chance2 == 10 or chance2 == 6 or chance2 == 7 or chance2 == 8 then
			Player.Functions.RemoveItem('cocaine_bag', 1)----change this
			TriggerClientEvent("inventory:client:ItemBox", source, QBCore.Shared.Items['cocaine_bag'], "remove")
			Player.Functions.AddMoney("cash", Config.Pricesell, "sold-pawn-items")
			TriggerClientEvent('QBCore:Notify', src, 'You Sold Cocaine', "success")   
		end 
	else
		TriggerClientEvent('QBCore:Notify', src, 'You don\'t have the right items', "error") 
	end
else
	TriggerClientEvent('QBCore:Notify', src, 'You don\'t have the right items', "error") 
	
end
end
end)



function CancelProcessing(playerId)
	if playersProcessingCannabis[playerId] then
		ClearTimeout(playersProcessingCannabis[playerId])
		playersProcessingCannabis[playerId] = nil
	end
end

RegisterServerEvent('qb-coke:cancelProcessing3')
AddEventHandler('qb-coke:cancelProcessing3', function()
	CancelProcessing(source)
end)

AddEventHandler('QBCore_:playerDropped', function(playerId, reason)
	CancelProcessing(playerId)
end)

RegisterServerEvent('qb-coke:onPlayerDeath2')
AddEventHandler('qb-coke:onPlayerDeath2', function(data)
	local src = source
	CancelProcessing(src)
end)

QBCore.Functions.CreateCallback('poppy:process', function(source, cb)
	local src = source
	local Player = QBCore.Functions.GetPlayer(src)
	 
	if Player.PlayerData.item ~= nil and next(Player.PlayerData.items) ~= nil then
		for k, v in pairs(Player.PlayerData.items) do
		    if Player.Playerdata.items[k] ~= nil then
				if Player.Playerdata.items[k].name == "cocaine_bag" then
					cb(true)
			    else
					TriggerClientEvent("QBCore:Notify", src, "You do not have any Coke process", "error", 10000)
					cb(false)
				end
	        end
		end	
	end
end)

QBCore.Functions.CreateUseableItem ('razor_blade', function(source)
    local xPlayer = QBCore.Functions.GetPlayer(source)
    if xPlayer.Functions.GetItemByName('kilo_coke').count > 0 then
        xPlayer.Functions.RemoveItem('kilo_coke', 1)
        Citizen.Wait(1000)
        xPlayer.Functions.AddItem('cokebaggy', 10)
    else
        print('My count is less than 1')
    end
end)

This particular line of code is just sitting inside a resource we are not really using. I wasnt sure where to put it but had read it can be anywhere.

Just trying to break this coke down without having to use a crafting table

I haven’t been able to test the code and going by memory, but try change “count” to “amount”:

QBCore.Functions.CreateUseableItem("razor_blade", function(source, item)
	local source = source
	local xPlayer = QBCore.Functions.GetPlayer(source)
	
	if not xPlayer.Functions.GetItemByName(item.name) then return end
	if xPlayer.Functions.GetItemByName("kilo_coke").amount > 0 then
		xPlayer.Functions.RemoveItem("kilo_coke", 1)
		Wait(0)
		xPlayer.Functions.AddItem("cokebaggy", 10)
	else
        print('My count is less than 1')
	end
end)

that did not work unfortunately

QBCore.Functions.CreateUseableItem("razor_blade", function(source, item)
	local source = source
	local xPlayer = QBCore.Functions.GetPlayer(source)
	
	if not xPlayer.Functions.GetItemByName(item.name) then return end
	if xPlayer.Functions.GetItemByName("kilo_coke").amount > 0 then
		print("My count is more than 0")
		xPlayer.Functions.RemoveItem("kilo_coke", 1)
		Wait(0)
		xPlayer.Functions.AddItem("cokebaggy", 10)
	else
        print('My count is less than 1')
	end
end)

Are you seeing any prints in the console?

Nothing on the console. resource is started on its own. I have both items in my inventory as well. I click to use the coke, it says used but nothing happens.

Are you looking in the client console or server console? And to confirm, you’re using qb-inventory?

I am in the client console. Yes we are using QB-inventory.

Check your server console.

Nothing in the console. No idea what the problem is

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.