Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.lua #22

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
function GetItemFromShop(itemName, zone)
local zoneItems = Config.Zones[zone].Items
local item = nil
local function GetItemFromShop(itemName, zone)
local ShopItemsByZone = Config.Zones[zone].Items

for _, itemData in pairs(zoneItems) do
for i = 1, #ShopItemsByZone do
local itemData = ShopItemsByZone[i]
if itemData.name == itemName then
item = itemData
break
return true, itemData.price, itemData.label
end
end

if not item then
return false
end
end

return true,item.price, item.label
return false
end

RegisterServerEvent('esx_shops:buyItem')
AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone)
RegisterNetEvent('esx_shops:buyItem', function(itemName, amount, zone)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local Exists, price, label = GetItemFromShop(itemName, zone)
amount = ESX.Math.Round(amount)

if amount < 0 then
print(('[^3WARNING^7] Player ^5%s^7 attempted to exploit the shop!'):format(source))
return
end
local Exists, price, label, amount = GetItemFromShop(itemName, zone), ESX.Math.Round(amount)

if not Exists then
if amount < 0 or not Exists then
print(('[^3WARNING^7] Player ^5%s^7 attempted to exploit the shop!'):format(source))
return
end

if Exists then
price = price * amount
-- can the player afford this item?
if xPlayer.getMoney() >= price then
-- can the player carry the said amount of x item?
local money = xPlayer.getMoney()
if money >= price then
if xPlayer.canCarryItem(itemName, amount) then
xPlayer.removeMoney(price, label .. " " .. TranslateCap('purchase'))
xPlayer.addInventoryItem(itemName, amount)
Expand All @@ -46,7 +33,7 @@ AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone)
xPlayer.showNotification(TranslateCap('player_cannot_hold'))
end
else
local missingMoney = price - xPlayer.getMoney()
local missingMoney = price - money
xPlayer.showNotification(TranslateCap('not_enough', ESX.Math.GroupDigits(missingMoney)))
end
end
Expand Down