Files
esx_core/server/main.lua
T
2021-04-26 23:17:56 +01:00

48 lines
1.2 KiB
Lua

ESX = nil
local ShopItems = {}
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
RegisterServerEvent('esx_shops:buyItem')
AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
amount = ESX.Math.Round(amount)
if amount < 0 then
print('esx_shops: ' .. xPlayer.identifier .. ' attempted to exploit the shop!')
return
end
-- get price
local price = 0
local itemLabel = ''
for i=1, #Config.Zones[zone].Items, 1 do
local item = Config.Zones[zone].Items[i]
if item.name == itemName then
price = item.price
itemLabel = item.label
break
end
end
price = price * amount
-- can the player afford this item?
if xPlayer.getMoney() >= price then
-- can the player carry the said amount of x item?
if xPlayer.canCarryItem(itemName, amount) then
xPlayer.removeMoney(price)
xPlayer.addInventoryItem(itemName, amount)
xPlayer.showNotification(_U('bought', amount, itemLabel, ESX.Math.GroupDigits(price)))
else
xPlayer.showNotification(_U('player_cannot_hold'))
end
else
local missingMoney = price - xPlayer.getMoney()
xPlayer.showNotification(_U('not_enough', ESX.Math.GroupDigits(missingMoney)))
end
end)