Need help ! Trying to figure out how to create a useable item that transform another

Hello ! :slight_smile:
Soooo here’s the deal,

I’m trying to figure out how to create an item that will transform another. For example … If i’d like to use rolling paper to roll up weed and give a joint. A useable item from the inventory HUD…

I’m pretty new at scripting and all that so i’m quite lost ! and i looked for tutorials or help but all i find is how to add basic need items like food and drinks that u can consume.

At first i tried to modify the code for those basicneeds items in an attempt. Here’s what i tried:

ESX.RegisterUsableItem(‘rollingpaper’, function(source)
local xPlayer = ESX.GetPlayerFromId(source)

xPlayer.removeInventoryItem('weed', 1)
xPlayer.removeInventoryItem('rollingpaper', 1)
xPlayer.addInventoryItem('joint', 1)

end)

But yeaahh … that didn’t work and i dont really know what else i’d need to add in client files etc… If someone can refer me to a guide or give me an example that i could modify it would be amazing :smiley:

What exactly didn’t work with this? If esx is not nil and you have the items in your database and inventory it should remove the weed and rolling paper then give you a joint, although its prob best to add some checks to make sure you have these items in your inventory before trying to remove them. I could try help you with that if you would like?

Hi! Maybe try to put a Citizen.Wait(0) between the remove and addInventoryItem, or trigger a message. I checked the code, should be working.

That’d be awesome ! :slight_smile: And i just couldnt use the item at all, nothing happened. I’ve set that code in the basic needs script tho … maybe i have to make a script only for that? but then i have no clue what to put as a client file, server file and ressource etc :o

Did you restart the server after adding this usable item to your resource? Is there any errors in the server console?

I did ! and no there was no errors.

Let me show you, i’ll set it again and i will show u screenshots :smiley: gimme a minute

There :slight_smile: so here’s what i wrote. I only wrote this in the server.lua of the optional needs script. I don’t know if i gotta add something in the client?

So as you can see my items exist already in the server. If i slide the mortar to the ‘’ use ‘’ button in inventory hud then nothing happens and i get no db or client errors :confused: it just does nothing at all

1 2

if you check my screenshot above, did i place the citizen wait in the right place?

Have you wrote the ESX stuff onto the server side file? I’ll write you the code:

ESX = nil
TriggerEvent(‘esx:getSharedObject’, function(obj)
ESX = obj
end)

Thats the whole server file. Like i said, i added the mortar part to esx_optionalneeds … Should i make a whole script just for the use of the mortar ? Since its not a drink or a food to go in basic or optional needs? I added what you said at the top but it still doesnt work

:sweat_smile:

oh wait i saw this sneaking in there… it didnt give me red errors… just saw this when i scrolled up to look

ok nvm that its just when i copied ur script part for

ESX = nil
TriggerEvent(‘esx:getSharedObject’, function(obj)
ESX = obj
end)

its wasnt ’ ’ :stuck_out_tongue: i changed it… removed the error … but its still not working haha

Try to add maybe to remove the mortar and then add a mortar in the end.

1 Like

Its works !! :smiley: Thank you so so much :heart_eyes:

Oh oops… Lol i have to add something to make it unable to be used if u dont have enough items because if i keep using it keeps giving me powder lmao

ESX.RegisterUsableItem('mortar', function(source)
    local xPlayer = ESX.GetPlayerFromId(source)
    
    if xPlayer.getInventoryItem('cocaleaves').count > 0 then
        xPlayer.removeInventoryItem('cocaleaves', 1)
        Citizen.Wait(0)
        xPlayer.addInventoryItem('cocapowder', 1)
    end
end)

ok :smiley: let me give it a try

Works perfect ! Thank you ! :smiley:

thanks both of you, you guys helped me a great deal :slight_smile:

oh ! i’d need the mortar for multiple items i tried to duplicate the script with the items changed but that gave me an error and this doesnt seem to work:

TriggerEvent(‘esx:getSharedObject’, function(obj)
ESX = obj
end)

ESX.RegisterUsableItem(‘mortar’, function(source)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.getInventoryItem('cocaleaves').count > 0 then
    xPlayer.removeInventoryItem('cocaleaves', 10)
    Citizen.Wait(0)
    xPlayer.addInventoryItem('cocapowder', 1)
	
or else	

 if   xPlayer.getInventoryItem('opium').count > 0 then
    xPlayer.removeInventoryItem('opium', 10)
    Citizen.Wait(0)
    xPlayer.addInventoryItem('poppypowder', 1)

end

end)

what am i doing wrong?

You can’t write:

or else

I’ll fix you the code:

TriggerEvent(‘esx:getSharedObject’, function(obj)
ESX = obj
end)

ESX.RegisterUsableItem(‘mortar’, function(source)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer.getInventoryItem(‘cocaleaves’).count > 0 then
xPlayer.removeInventoryItem(‘cocaleaves’, 10)
Citizen.Wait(0)
xPlayer.addInventoryItem(‘cocapowder’, 1)

ESX.RegisterUsableItem(‘mortar2’, function(source)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer.getInventoryItem(‘opium’).count > 0 then
xPlayer.removeInventoryItem(‘opium’, 10)
Citizen.Wait(0)
xPlayer.addInventoryItem(‘poppypowder’, 1)
end
end)