First of all there are many methods to get the closest ped/entity/whatever to your character, however they have some issues:
- They only are able to find things within a small radius.
- They use broken native methods such as GetClosestPed which do not get the correct result.
Here i will show you a way to get the closest entity to you, without any of those issues ocurring (hopefully).
For an improved version of the script i reccomend checking this out.
function GetClosestEntity(entitytype, alive, targets, los, ignoreanimals, coords)
-- Usage: GetClosestEntity("CPed", "alive", "any", "inlos", false, {x, y, z})
-- entitytype accepts the following arguments: CPed, CObject, CVehicle, CPickup
-- alive accepts the following arguments: "any", "alive", "dead" Vehicle Only: "alivedriver", "deaddriver"
-- targets accepts the following arguments: "any", "player", "npc" or "empty" (Vehicle only!)
-- los accepts the following arguments: "any", "notinlos", "inlos"
-- coords accepts the following arguments: {x,y,z} or you can omit it and it will use the player's coords
-- ignoreanimals is just a simple boolean
-- Tested params: entitytype,alive,los,ignoreanimals,coords
local entitytypeParam = entitytype or "CPed"
-- Get only alive or dead entities according to the alive param
local aliveParam = alive or "alive"
-- Get entities that are only players or npcs or both.
local targetsParam = targets or "any"
-- Get only entities in or out of your line of sight according to the los param
local losParam = los or "inlos"
-- Ignore entities that are animals according to the ignoreanimals param
local ignoreanimalsParam = ignoreanimals or false
-- Get specified coords or coords of current player if empty
local coordsparam = coords or GetEntityCoords(PlayerPedId())
local closestEntity = -1
local closestDist = -1
local aliveCheck = -1
local losCheck = -1
-- Get Game Pool according to type:
local entityPool = GetGamePool(entitytypeParam)
-- Loop through the game pool until closest entity is found.
for i = 1, #entityPool do
entity = entityPool[i]
-- Handles the alive param
aliveCheck = (aliveParam == "alive" and not IsEntityDead(entity)) or (aliveParam == "dead" and IsEntityDead(entity)) or (aliveParam == "alivedriver" and entitytypeParam == "CVehicle" and not IsEntityDead(GetPedInVehicleSeat(entity, -1)) and not IsVehicleSeatFree(entity, -1)) or (aliveParam == "deaddriver" and entitytypeParam == "CVehicle" and IsEntityDead(GetPedInVehicleSeat(entity, -1)) and not IsVehicleSeatFree(entity, -1)) or (aliveParam == "any")
-- Handles the targets param
targetsCheck = (targetsParam == "player" and not entitytypeParam == "CVehicle" and IsPedAPlayer(entity)) or (targetsParam == "npc" and not entitytypeParam == "CVehicle" and not IsPedAPlayer(entity)) or (targetsParam == "npc" and entitytypeParam == "CVehicle" and not IsPedAPlayer(GetPedInVehicleSeat(entity, -1)) and not IsVehicleSeatFree(entity, -1)) or (targetsParam == "player" and entitytypeParam == "CVehicle" and IsPedAPlayer(GetPedInVehicleSeat(entity, -1))) or (targetsParam == "empty" and entitytypeParam == "CVehicle" and IsVehicleSeatFree(entity, -1) and GetVehicleNumberOfPassengers(entity) == 0 and GetPedInVehicleSeat(entity,-1) == 0) or (targetsParam == "any")
-- Handles the los param
losCheck = (losParam == "inlos" and HasEntityClearLosToEntity(PlayerPedId(), entity, 17)) or (losParam == "notinlos" and not HasEntityClearLosToEntity(PlayerPedId(), entity, 17)) or (losParam == "any")
-- Handles the ignoreanimals param
ignoreanimalsCheck = (ignoreanimalsParam and IsEntityAPed(entity) and GetPedType(entity) ~= 28) or (not ignoreanimalsParam)
if entity ~= PlayerPedId() and aliveCheck and losCheck and targetsCheck and ignoreanimalsCheck then
local distance = #(coordsparam - GetEntityCoords(entity))
if closestDist == -1 or distance < closestDist then
closestEntity = entity
closestDist = distance
end
end
end
return closestEntity, closestDist
end
Example Usage:
local ent, dist = GetClosestEntity("CPed", "alive", "any", "inlos", false)
print(ent,dist)
if IsEntityAVehicle(ent) then
print("Entity is a veh")
elseif IsEntityAPed(ent) then
print("Entity is a ped")
elseif IsEntityAnObject(ent) then
print("Entity is a object")
end
if IsEntityDead(ent) then
print("Entity is dead")
else
print("Entity is alive")
end
This code might be a bit crap and unoptimized but im always happy for some constructive criticism if you find a way to improve it!
Thanks to Cozy on the cfx.re discord and @DrAceMisanthrope for helping me out with this!