Entities

CLIENT SIDE ONLY

A small selection of usefull functions relevant to entities within FiveM.

Functions

get_nearby_objects

Retrieves list of nearby objects.

Function

local function get_nearby_objects(coords, max_distance)
    return get_nearby_entities('CObject', coords, max_distance)
end

exports('get_nearby_objects', get_nearby_objects)
utils.entities.get_nearby_objects = get_nearby_objects

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0

--- Utils object
local objects = utils.entities.get_nearby_objects(coords, radius)

for _, obj in ipairs(objects) do
    print("Object ID:", obj.entity)
    print("Object Coordinates:", obj.coords)
end

--- Direct export
local objects = exports.boii_utils:get_nearby_objects(...)

get_nearby_peds

Retrieves list of nearby peds.

Function

local function get_nearby_peds(coords, max_distance)
    return get_nearby_entities('CPed', coords, max_distance, function(ped)
        return not IsPedAPlayer(ped)
    end)
end

exports('get_nearby_peds', get_nearby_peds)
utils.entities.get_nearby_peds = get_nearby_peds

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0

--- Utils object
local nearby_peds = utils.entities.get_nearby_peds(coords, radius)

for _, ped in ipairs(nearby_peds) do
    print("Ped ID:", ped.entity)
    print("Ped Coordinates:", ped.coords)
end

--- Direct export
exports.boii_utils:get_nearby_peds(...)

get_nearby_players

Retrieves a list of nearby players.

Function

local function get_nearby_players(coords, max_distance, include_player)
    local player_id = PlayerId()
    return get_nearby_entities('CPed', coords, max_distance, function(ped)
        local ped_player_id = NetworkGetEntityOwner(ped)
        return include_player or ped_player_id ~= player_id
    end)
end

exports('get_nearby_players', get_nearby_players)
utils.entities.get_nearby_players = get_nearby_players

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0
--- Specify if the player should be included
local include_player = true

--- Utils object
local nearby_players = utils.entities.get_nearby_players(coords, radius, include_player)

for _, player in ipairs(nearby_players) do
    print("Pplayer ID:", player.entity)
    print("Ped Coordinates:", player.coords)
end

--- Direct export
exports.boii_utils:get_nearby_players(...)

get_nearby_vehicles

Retrieve list of nearby vehicles optionally include players vehicle.

Function

local function get_nearby_vehicles(coords, max_distance, include_player_vehicle)
    local player_vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    return get_nearby_entities('CVehicle', coords, max_distance, function(vehicle)
        return include_player_vehicle or vehicle ~= player_vehicle
    end)
end

exports('get_nearby_vehicles', get_nearby_vehicles)
utils.entities.get_nearby_vehicles = get_nearby_vehicles

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0
--- Specify if the player should be included
local include_player_vehicle = true

--- Utils object
local vehicles = utils.entities.get_nearby_vehicles(coords, radius, include_player_vehicle)

for _, vehicle in ipairs(vehicles) do
    print("Vehicle ID:", vehicle.entity)
    print("Vehicle Coordinates:", vehicle.coords)
end

--- Direct export
exports.boii_utils:get_nearby_vehicles(...)

get_closest_object

Retrieves the closest object.

Function

local function get_closest_object(coords, max_distance)
    return get_closest_entity('CObject', coords, max_distance)
end

exports('get_closest_object', get_closest_object)
utils.entities.get_closest_object = get_closest_object

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0

--- Utils object
local closest_object, object_coords = utils.entities.get_closest_object(coords, radius)
if closest_object then
    print("Closest Object ID:", closest_object)
    print("Closest Object Coordinates:", object_coords)
else
    print("No objects found within the specified distance.")
end

--- Direct export
exports.boii_utils:get_closest_object(...)

get_closest_ped

Retrieves the closest ped excluding players.

Function

local function get_closest_ped(coords, max_distance)
    return get_closest_entity('CPed', coords, max_distance, function(ped)
        return not IsPedAPlayer(ped)
    end)
end

exports('get_closest_ped', get_closest_ped)
utils.entities.get_closest_ped = get_closest_ped

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0

--- Utils object
local closest_ped, ped_coords = utils.entities.get_closest_ped(coords, radius)
if closest_ped then
    print("Closest Ped ID:", closest_ped)
    print("Closest Ped Coordinates:", ped_coords)
else
    print("No peds found within the specified distance.")
end

--- Direct export
exports.boii_utils:get_closest_ped(...)

get_closest_player

Retrieves the closest player.

Function

local function get_closest_player(coords, max_distance, include_player)
    return get_closest_entity('CPed', coords, max_distance, function(ped)
        local ped_player_id = NetworkGetEntityOwner(ped)
        return include_player or ped_player_id ~= PlayerId()
    end)
end

exports('get_closest_player', get_closest_player)
utils.entities.get_closest_player = get_closest_player

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0
--- Specify if the player should be included
local include_player = true


--- Utils object
local closest_player, player_ped, player_coords = utils.entities.get_closest_player(coords, radius, include_player)
if closest_player then
    print("Closest Player ID:", closest_player)
    print("Closest Player Ped:", player_ped)
    print("Closest Player Coordinates:", player_coords)
else
    print("No players found within the specified distance.")
end

--- Direct export
exports.boii_utils:get_closest_player(...)

get_closest_vehicle

Retrieve the closest vehicle.

Function

local function get_closest_vehicle(coords, max_distance, include_player_vehicle)
    return get_closest_entity('CVehicle', coords, max_distance, function(vehicle)
        return include_player_vehicle or vehicle ~= GetVehiclePedIsIn(PlayerPedId(), false)
    end)
end

exports('get_closest_vehicle', get_closest_vehicle)
utils.entities.get_closest_vehicle = get_closest_vehicle

Example

--- Define coords
local coords = vector3(100.0, 100.0, 20.0)
--- Define radius
local radius = 10.0
--- Specify if players vehicle should be included
local include_player_vehicle = true

--- Utils object
local closest_vehicle, vehicle_coords = utils.entities.get_closest_vehicle(coords, radius, include_player_vehicle)
if closest_vehicle then
    print("Closest Vehicle ID:", closest_vehicle)
    print("Closest Vehicle Coordinates:", vehicle_coords)
else
    print("No vehicles found within the specified distance.")
end

--- Direct export
exports.boii_utils:get_closest_vehicle(...)

get_entities_infront_of_player

Gets entities in front of player within a specified FOV and distance.

Function

local function get_entities_in_front_of_player(fov, distance)
    local player = PlayerPedId()
    local player_coords = GetEntityCoords(player)
    local forward_vector = GetEntityForwardVector(player)
    local end_coords = vector3(
        player_coords.x + forward_vector.x * distance,
        player_coords.y + forward_vector.y * distance,
        player_coords.z + forward_vector.z * distance
    )
    local hit, _, _, _, entity = StartShapeTestRay(
        player_coords.x, player_coords.y, player_coords.z,
        end_coords.x, end_coords.y, end_coords.z, -1, player, 0
    )
    return hit and GetEntityType(entity) ~= 0 and entity or nil
end

exports('get_entities_in_front_of_player', get_entities_in_front_of_player)
utils.entities.get_entities_in_front_of_player = get_entities_in_front_of_player

Example

--- Define FOV
local FOV = 45.0
--- Define distance
local distance = 10.0

--- Utils object
local entity_in_front = utils.entities.get_entities_in_front_of_player(FOV, distance)
if entity_in_front then
    print("Entity in front of player:", entity_in_front)
else
    print("No entity found in front of the player within the specified FOV and distance.")
end

--- Direct export
exports.boii_utils:get_entities_infront_of_player(...)

get_target_ped

Gets the target ped or nearest ped.

Function

local function get_target_ped(player_ped, fov, distance)
    local entity = get_entities_in_front_of_player(fov, distance)
    if entity and IsEntityAPed(entity) and not IsPedAPlayer(entity) then
        return entity, GetEntityCoords(entity)
    end
    return get_closest_ped(GetEntityCoords(player_ped), distance)
end

exports('get_target_ped', get_target_ped)
utils.entities.get_target_ped = get_target_ped

Example

--- Define FOV
local FOV = 45.0
--- Define distance
local distance = 10.0

--- Utils object
local target_ped, target_coords = utils.entities.get_target_ped(PlayerPedId(), FOV, distance)
if target_ped then
    print("Target Ped ID:", target_ped)
    print("Target Ped Coordinates:", target_coords)
else
    print("No target ped found within the specified FOV or distance.")
end

--- Direct export
exports.boii_utils:get_target_ped(...)

Last updated