Hello
Im searching for a simple script where you can sell items and get money (ESX)
This is the snippet I use for this. It’s a modified version of the drug dealer from esx_drugs but It uses 3d text and an NPC that you sell the item to. Obviously you would have to change the items and the locations etc.
Client.lua
local ScrapTrader = vector3(-429.33,-1728.33,19.78)
ScrapTraderPrices = {
aluminium = 100,
airbag = 250,
radio = 600,
wheel_rim = 300,
battery = 440,
}
local menuOpen = false
local wasOpen = false
Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(0)
end
while ESX.GetPlayerData().job == nil do
Citizen.Wait(100)
end
ESX.PlayerData = ESX.GetPlayerData()
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
local playerPed = PlayerPedId()
local coords = GetEntityCoords(playerPed)
local dist = #(vector3(ScrapTrader['x'],ScrapTrader['y'],ScrapTrader['z']) - coords)
if dist <= 2 then
DrawText3Ds(ScrapTrader['x'],ScrapTrader['y'],ScrapTrader['z'],"Press ~r~[E]~s~ to talk with the ~r~ Scrap Trader")
if not menuOpen then
if IsControlJustReleased(0, 38) then
wasOpen = true
OpenScrapShop()
end
else
Citizen.Wait(500)
end
end
end
end)
function OpenScrapShop()
ESX.UI.Menu.CloseAll()
local elements = {}
menuOpen = true
for k, v in pairs(ESX.GetPlayerData().inventory) do
local price = ScrapTraderPrices[v.name]
if price and v.count > 0 then
table.insert(elements, {
label = ('%s - <span style="color:green;">%s</span>'):format(v.label, '$'..price, ESX.Math.GroupDigits(price)),
name = v.name,
price = price,
type = 'slider',
value = 1,
min = 1,
max = v.count
})
end
end
ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'scrap_trader', {
title = 'Scrap Trader',
align = 'top-left',
elements = elements
}, function(data, menu)
TriggerServerEvent('sellScrap', data.current.name, data.current.value)
end, function(data, menu)
menu.close()
menuOpen = false
end)
end
AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
if menuOpen then
ESX.UI.Menu.CloseAll()
end
end
end)
--Creates NPC for Scrap Trader
Citizen.CreateThread(function()
npcHash = GetHashKey("mp_m_waremech_01")
RequestModel(npcHash)
while not HasModelLoaded(npcHash) do
Wait(1)
end
npc = CreatePed(1, npcHash, -429.33,-1728.33,18.78, 79.12, false, true)
SetBlockingOfNonTemporaryEvents(npc, true)
SetPedDiesWhenInjured(npc, false)
SetPedCanPlayAmbientAnims(npc, true)
SetPedCanRagdollFromPlayerImpact(npc, false)
SetEntityInvincible(npc, true)
FreezeEntityPosition(npc, true)
TaskStartScenarioInPlace(npc, "WORLD_HUMAN_SMOKING", 0, true);
end)
--End of Scrap Trader
DrawText3Ds = function(x,y,z, text)
local onScreen,_x,_y=World3dToScreen2d(x,y,z)
local factor = #text / 370
local px,py,pz=table.unpack(GetGameplayCamCoords())
SetTextScale(0.35, 0.35)
SetTextFont(4)
SetTextProportional(1)
SetTextColour(255, 255, 255, 215)
SetTextEntry("STRING")
SetTextCentre(1)
AddTextComponentString(text)
DrawText(_x,_y)
DrawRect(_x,_y + 0.0125, 0.015 + factor, 0.03, 0, 0, 0, 120)
end
Server.lua
ESX = nil
local GiveDirty = true
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
ScrapTraderPrices = {
aluminium = 100,
airbag = 250,
radio = 600,
wheel_rim = 300,
battery = 440,
}
RegisterServerEvent('sellScrap')
AddEventHandler('sellScrap', function(itemName, amount)
local xPlayer = ESX.GetPlayerFromId(source)
local price = ScrapTraderPrices[itemName]
local xItem = xPlayer.getInventoryItem(itemName)
if not price then
print(('mojito-esx_chopshop %s attempted to sell an invalid item!'):format(xPlayer.identifier))
return
end
if xItem.count < amount then
xPlayer.showNotification("~r~You do not have enough of this item.")
return
end
price = ESX.Math.Round(price * amount)
if GiveDirty then
xPlayer.addAccountMoney('black_money', price)
else
xPlayer.addMoney(price)
end
xPlayer.removeInventoryItem(xItem.name, amount)
xPlayer.showNotification('you\'ve sold ~b~'..amount..'x~s~ ~y~'..xItem.name..'~s~ for ~g~$'..price..'~s~', amount, xItem.label, ESX.Math.GroupDigits(price))
end)
thanks man!
Can you please send me the full script file and tell me what to start it as please thank you?
If you just want to sell some items, that’s the full script there.
Thank you what do I start it as and what do I name the folder?
Well that’s the code, you need to implement it like any other resource. You will need to make a client file, a server file and an fxmanifest.
Thanks I’m no developer lol I just read the forums to try to learn but don’t no how to put scripts together thank you though
A mojito question, how can I enter the quantity and not have to select it.
can you make an opposite script ?
i need one to buy items…
I need help there is no shop showing up i dindt change anything the npc/shop is not there
Nice Necropost
what?
You’re commenting on a 2 year old thread dude
and i requested help? why so rude man