[help] Fixing GetItemByName nil value for custom script

Hey there, new to posting here so sorry If I mess something up

I’ve been bugfixing/developing for a server for about a month now and picked up some things, I mainly worked on other peoples scripts to adapt them or fix them.

But anyway, I recently decided to make my own mining script to test my own knowledge
It’s base is QBCore but I’m running into some annoying issues. It uses bt-target for selecting points and nh-context for choosing options.

Basically my issues are based in the server.lua, whenever I try to grab the item by name “GetItemByName” it claims its a nil value. Which is painfully anying when you need to grab item amounts for selling.
Other commands like additem and removeitem work, but if i try and pull information it throws out errors.
I’ve tried many different ways and it won’t work…so I assume I’m missing something glaringly obvious

can anyone take a look and let me know how to fix it and let it grab inventory info?

The code mainly in question is:

RegisterNetEvent('jim-mining:SellOre')
AddEventHandler('jim-mining:SellOre', function(data)
    local src = source
	local Player = QBCore.Functions.GetPlayer(source)
	if data.id == 1 then
		local Ore = Player.Functions.GetItemByName("copperore")
	if data.id == 2 then
	    local Ore = Player.Functions.GetItemByName("ironore")
	if data.id == 3 then
		local Ore = Player.Functions.GetItemByName("goldore")
	if data.id == 4 then
		local Ore = Player.Functions.GetItemByName("carbon")
	end
	
    TriggerClientEvent("inventory:client:ItemBox", source, QBCore.Shared.Items[Ore], "remove", Ore.amount)
	
	TriggerNetEvent('jim-mining:SellAnim')
end)

When it goes to show the item box it claims it returns a nil value in the server log
But the data.id does seem to be called correctly

xPlayer.getInventoryItem -> xPlayer.Functions.GetItemByName

seems correct but if is it nill that mean he dont know what is it. I am not QBcore userbut did you put your items to QB_core (not sure shared.lua) I mean into file where are the items?

Also you speak about selling issue so you want to remove item not get you dont givint item to server you erasing it so I gess here is the answer

xPlayer.removeInventoryItem -> xPlayer.Functions.RemoveItem (item name)

Oh, I can remove and items items to and from the inventory, but I can’t retrieve the info of how many are in the users pockets.

And sadly I started out on QBCore so it’s all I know for now

GetItemCount

But I think this dont make your script, but qb_Inventory script who cares about how many or how much
So If I doing it I trigger some inventory to remove some amount or all

I am not PRO so I just saying here some my opinion what is not relevant.

Because I need to know if there are any in inventory before selling?

If they are selling and it’s not looking for if they actually have any or not, it’ll give money and take nothing.

try to check it from qb_pawnShop… there will be thic function what you looking for

I managed to fix it on my own, after a long sleep

I was unfortunately thinking too small with it
I was trying to make it dynamic and filling up “Ore” with the info needed but had to separate it a bit and it suddenly understood what I was asking.

RegisterServerEvent('jim-mining:SellOre')
AddEventHandler('jim-mining:SellOre', function(data)
    local src = source
	local Player = QBCore.Functions.GetPlayer(source)
	if data == 1 then
        if Player.Functions.GetItemByName('copperore') ~= nil then
            ore = Player.Functions.GetItemByName('copperore').amount
            pay = (ore * Config.SellItems['copperore'])
            Player.Functions.RemoveItem("copperore", ore)
            Player.Functions.AddMoney('cash', pay)
            TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items["copperore"], 'remove', ore)
        else
        TriggerClientEvent('QBCore:Notify', source, "You don't have any copper ore to sell.", "error")
        end
	elseif data == 2 then
        if Player.Functions.GetItemByName('ironore') ~= nil then
            ore = Player.Functions.GetItemByName('ironore').amount
            pay = (ore * Config.SellItems['ironore'])
            Player.Functions.RemoveItem("ironore", ore)
            Player.Functions.AddMoney('cash', pay)
            TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items["ironore"], 'remove', ore)
        else
        TriggerClientEvent('QBCore:Notify', source, "You don't have any iron ore to sell.", "error")
        end
	elseif data == 3 then
        if Player.Functions.GetItemByName('goldore') ~= nil then
            ore = Player.Functions.GetItemByName('goldore').amount
            pay = (ore * Config.SellItems['goldore'])
            Player.Functions.RemoveItem("goldore", ore)
            Player.Functions.AddMoney('cash', pay)
            TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items["goldore"], 'remove', ore)
        else
        TriggerClientEvent('QBCore:Notify', source, "You don't have any gold ore to sell.", "error")
        end
	elseif data == 4 then
        if Player.Functions.GetItemByName('carbon') ~= nil then
            ore = Player.Functions.GetItemByName('carbon').amount
            pay = (ore * Config.SellItems['carbon'])
            Player.Functions.RemoveItem("carbon", ore)
            Player.Functions.AddMoney('cash', pay)
            TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items["carbon"], 'remove', ore)
        else
        TriggerClientEvent('QBCore:Notify', source, "You don't have any carbon to sell.", "error")
        end
	end
end)
1 Like

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