Advertisement
userMacieG

Untitled

Dec 15th, 2023 (edited)
1,507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. -- client
  2. exports.ox_target:addGlobalPlayer({
  3.     label = "Rob",
  4.     name = 'script:rob',
  5.     distance = 2.5,
  6.     canInteract = function()
  7.         -- here you can add checks when player can be robbed
  8.         return true
  9.     end
  10.     onSelect = function(data)
  11.         local targetPed = data.entity
  12.         local targetNetworkId = NetworkGetPlayerIndexFromPed(targetPed)
  13.         local targetServerId = GetPlayerServerId(targetNetworkId)
  14.  
  15.         TriggerServerEvent('script:server:rob', targetServerId)
  16.     end
  17. })
  18.  
  19. -- server
  20. local robbing = {}
  21. local blacklistedItems = {
  22.     'water'
  23. }
  24.  
  25. RegisterNetEvent('script:server:rob', function(targetId)
  26.     local src = source
  27.     local Player = QBCore.Functions.GetPlayer(src)
  28.  
  29.     if not Player then
  30.         -- player cant be nil
  31.         return
  32.     end
  33.  
  34.     local Target = QBCore.Functions.GetPlayer(targetId)
  35.  
  36.     if not Target then
  37.         -- target cant be nil
  38.         return
  39.     end
  40.  
  41.     local playerPed = GetPlayerPed(src)
  42.     local targetPed = GetPlayerPed(targetId)
  43.     local playerCoords = GetEntityCoords(playerPed)
  44.     local targetCoords = GetEntityCoords(targetPed)
  45.  
  46.     if #(playerCoords - targetCoords) > 2.5 then
  47.         -- target is too far away
  48.         return
  49.     end
  50.  
  51.     if robbing[src] then
  52.         -- already robbing
  53.         return
  54.     end
  55.        
  56.     exports.ox_inventory.forceOpenInventory(src, 'player', targetId)
  57.  
  58.     robbing[src] = targetId
  59. end)
  60.  
  61. RegisterNetEvent('ox_inventory:closedInventory', function(source, target)
  62.     if not robbing[source] then
  63.         return
  64.     end
  65.  
  66.     if robbing[source] ~= target then
  67.         return
  68.     end
  69.  
  70.     robbing[src] = nil
  71. end)
  72.  
  73. exports.ox_inventory:registerHook('swapItems', function(payload)
  74.     local source = payload.source
  75.  
  76.     if not robbing[source] then
  77.         return true
  78.     end
  79.  
  80.     if robbing[source] ~= payload.fromInventory then
  81.         return true
  82.     end
  83.  
  84.     local item = payload.fromSlot.name:lower()
  85.  
  86.     for k, v in pairs(blacklistedItems) do
  87.         if v == item then
  88.             return false
  89.         end
  90.     end
  91.  
  92.     return true
  93. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement