It looks like your code is mostly correct, but there is one issue that could be causing it not to work.
The GetRandomPedAtCoord function returns a random ped within a certain radius, so it’s possible that no peds are within the specified range of the player’s coordinates. In this case, the rped variable would be nil, and the DoesEntityExist check would fail.
To address this issue, you could try increasing the radius or using a different method to find nearby peds, such as the GetNearbyPeds function from the NativeUI library. Additionally, you may want to consider adding some debug messages or logging to help diagnose any issues.
Here’s an updated version of your code with some additional comments and debugging output:
ox_inventory:useItem(data, function(data)
-- Get the player's current position
local pos = GetEntityCoords(GetPlayerPed(-1))
-- Find the closest ped to the player
local rped = GetClosestPed(pos['x'], pos['y'], pos['z'], 20.05, 1, 0, 0, 0, -1)
-- Debugging output to check if a ped was found
if rped ~= nil then
print("Found ped " .. rped .. " within range")
else
print("No peds within range")
end
-- If a ped was found, make them flee from the player
if DoesEntityExist(rped) then
TaskSmartFleePed(rped, pos, 5000.0, 75, 0, 0)
end
end)
I am not familiar with the ox_inventory framework as I use my own custom inventory system. However, based on your code, I have inferred the general idea of what you were attempting to achieve. I have not yet tested this code, let me know if it works for you.