Advertisement
Nidhoggx

esx_shops server

May 3rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. ESX = nil
  2. local ItemsLabels = {}
  3.  
  4. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  5.  
  6. AddEventHandler('onMySQLReady', function()
  7.  
  8. MySQL.Async.fetchAll(
  9. 'SELECT * FROM items',
  10. {},
  11. function(result)
  12.  
  13. for i=1, #result, 1 do
  14. ItemsLabels[result[i].name] = result[i].label
  15. end
  16.  
  17. end
  18. )
  19.  
  20. end)
  21.  
  22. ESX.RegisterServerCallback('esx_shops:requestDBItems', function(source, cb)
  23.  
  24. MySQL.Async.fetchAll(
  25. 'SELECT * FROM shops',
  26. {},
  27. function(result)
  28. local shopItems = {}
  29. for i=1, #result, 1 do
  30. if shopItems[result[i].name] == nil then
  31. shopItems[result[i].name] = {}
  32. end
  33.  
  34. table.insert(shopItems[result[i].name], {
  35. name = result[i].item,
  36. price = result[i].price,
  37. label = ItemsLabels[result[i].item]
  38. })
  39. end
  40. cb(shopItems)
  41. end
  42. )
  43.  
  44. end)
  45.  
  46. RegisterServerEvent('esx_shops:buyItem')
  47. AddEventHandler('esx_shops:buyItem', function(itemName, price)
  48.  
  49. local _source = source
  50. local xPlayer = ESX.GetPlayerFromId(_source)
  51. local sourceItem = xPlayer.getInventoryItem(itemName)
  52.  
  53. -- can the player afford this item?
  54. if xPlayer.getMoney() >= price then
  55.  
  56. -- can the player carry the said amount of x item?
  57. if sourceItem.limit ~= -1 and (sourceItem.count + 1) > sourceItem.limit then
  58. TriggerClientEvent('esx:showNotification', _source, _U('player_cannot_hold'))
  59. else
  60. xPlayer.removeMoney(price)
  61. xPlayer.addInventoryItem(itemName, 1)
  62. TriggerClientEvent('esx:showNotification', _source, _U('bought', ItemsLabels[itemName], price))
  63. end
  64. else
  65. local missingMoney = price - xPlayer.getMoney()
  66. TriggerClientEvent('esx:showNotification', _source, _U('not_enough', missingMoney))
  67. end
  68.  
  69. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement