[ESX] [INVENTORYHUD] Delete Items?

Hey everyone!

I have a problem with the esx_inventoryhud. When i drop a item from the inventory, the item is on the ground and can be picked up again. I want that the item gets deletet when it drops. Any help maybe?

Hey ! :slightly_smiling_face:

I looked into it, the code that make the item able to be picked up when he is removed from the inventory isn’t in the [esx_inventoryhud] resource itself. The [client/main.lua] script Trigger an esx Event wich is set in an [es_extended] script (as you can see on the screens below).

The Event in [es_extended/server/main.lua]:


The code in the Event that make the Item pickable after removal in [es_extended/server/main.lua]:

What you can do is, create your own serverEvent that does prettymuch the same thing the only exception will be that you won’t make it pickable, you will only remove the item from the player’s inventory. To do so you juste have to re-use the [es_extended/server/main.lua] change few things and Trigger your own event.
I wrote the code for you, it should work even if its not perfect :wink:

To write in your [esx_inventoryhud\client\main.lua]

RegisterNUICallback(
    "DropItem",
    function(data, cb)
        if IsPedSittingInAnyVehicle(playerPed) then
            return
        end

        if type(data.number) == "number" and math.floor(data.number) == data.number then
            TriggerServerEvent("esx_inventoryhud:deleteItem", data.item.type, data.item.name, data.number)
        end

        Wait(250)
        loadPlayerInventory()

        cb("ok")
    end
)

and to write in your [esx_inventoryhud\server\main.lua]

RegisterNetEvent("esx_inventoryhud:deleteItem")
AddEventHandler("esx_inventoryhud:deleteItem", function(itemType, itemName, nb)
	local playerId = source
	local xPlayer = ESX.GetPlayerFromId(source)
	
	if itemType == 'item_standard' then
		if itemCount == nil or itemCount < 1 then
			xPlayer.showNotification(_U('imp_invalid_quantity'))
		else
			local xItem = xPlayer.getInventoryItem(itemName)

			if (itemCount > xItem.count or xItem.count < 1) then
				xPlayer.showNotification(_U('imp_invalid_quantity'))
			else
				xPlayer.removeInventoryItem(itemName, itemCount)
			end
		end
	elseif itemType == 'item_account' then
		if itemCount == nil or itemCount < 1 then
			xPlayer.showNotification(_U('imp_invalid_amount'))
		else
			local account = xPlayer.getAccount(itemName)

			if (itemCount > account.money or account.money < 1) then
				xPlayer.showNotification(_U('imp_invalid_amount'))
			else
				xPlayer.removeAccountMoney(itemName, itemCount)
			end
		end
	elseif itemType == 'item_weapon' then
		itemName = string.upper(itemName)

		if xPlayer.hasWeapon(itemName) then
			local _, weapon = xPlayer.getWeapon(itemName)
			local _, weaponObject = ESX.GetWeapon(itemName)
			local components, pickupLabel = ESX.Table.Clone(weapon.components)
			xPlayer.removeWeapon(itemName)

			if weaponObject.ammo and weapon.ammo > 0 then
				local ammoLabel = weaponObject.ammo.label
			else
				xPlayer.showNotification(_U('threw_weapon', weapon.label))
			end

		end
	end
	
end)

Edit: I had some hard time to paste my code on the post i don’t know why :sweat_smile: , don’t hesitate to verify the synthax if you copy/past it, their might be some syntax issues wich slip up when I paste my code and tried to preformate it :confused:

Peace

1 Like

What can i say… works perfect! Thank you very much for your help! love you :slight_smile:

1 Like

The only thing is that i cant remove “bread, phone” and other stuff. Weapon remove works great!
Do i have to add something to the code that the other items work?

1 Like

I’m going to look into it, I’ll get back to you :wink:

thank you very much :slight_smile:

I tweeked the code a little bit :

The main issue was comming from [itemCount] being nil or at 0 so we need to add [itemCout = nb] (line 83):

Their was another little issue (no notification when we drop weapon) so we need to add this (line 119) :

And finaly when the notifications where display (after a drop) their was an issue in the text display simply, because the code came from es_extended and was expecting to be translate according to the location (but since we dont have all the other files of the project it wont work). So we just need to remove the [_U()] from every notification, ex:

screen17

The tweeked code :

RegisterNetEvent("esx_inventoryhud:deleteItem")
AddEventHandler("esx_inventoryhud:deleteItem", function(itemType, itemName, nb)
	local playerId = source
	local xPlayer = ESX.GetPlayerFromId(source)
	itemCount = nb
	
	if itemType == 'item_standard' then
		if itemCount == nil or itemCount < 1 then
			xPlayer.showNotification('invalid quantity')
		else
			local xItem = xPlayer.getInventoryItem(itemName)

			if (itemCount > xItem.count or xItem.count < 1) then
				xPlayer.showNotification('invalid quantity')
			else
				xPlayer.removeInventoryItem(itemName, itemCount)
			end
		end
	elseif itemType == 'item_account' then
		if itemCount == nil or itemCount < 1 then
			xPlayer.showNotification('invalid amount')
		else
			local account = xPlayer.getAccount(itemName)

			if (itemCount > account.money or account.money < 1) then
				--xPlayer.showNotification(_U('imp_invalid_amount'))
				xPlayer.showNotification('invalid amount')
			else
				xPlayer.removeAccountMoney(itemName, itemCount)
			end
		end
	elseif itemType == 'item_weapon' then
		itemName = string.upper(itemName)

		if xPlayer.hasWeapon(itemName) then
			local _, weapon = xPlayer.getWeapon(itemName)
			local _, weaponObject = ESX.GetWeapon(itemName)
			local components, pickupLabel = ESX.Table.Clone(weapon.components)
			xPlayer.removeWeapon(itemName)
			xPlayer.showNotification('Threw weapon : '..weapon.label)

			if weaponObject.ammo and weapon.ammo > 0 then
				local ammoLabel = weaponObject.ammo.label
			else
				xPlayer.showNotification('threw weapon :'..weapon.label)
			end

		end
	end
	
end)

Here you go ! :grinning:

Peace

1 Like

Thank you again for your great and fast help! Now it works perfect! TY!

1 Like

No prob, see you around :wink:

1 Like

Server side, so must be :

RegisterServerEvent(“esx_inventoryhud:deleteItem”)

:smiley:

How would I go about adding this to my server. We are using AK47_inventory?

Hey, I’m not familiar with this inventory, but if you are using ESX then it is most likely the same thing as I wrote in my previous answers.

how do i get rid money the money bag when i drop items on the floor?