Entities
The Entities module provides comprehensive entity detection and management utilities for FiveM resources with advanced filtering, caching, and performance optimization.
getNearby
Gets nearby entities of any type with comprehensive filtering options.
B2Lib.Entities.getNearby(coords, radius, entityType, options)
Parameters:
coords
(vector3, optional): Coordinates to search from (default: player position)radius
(number, optional): Search radius in units (default: 50.0)entityType
(string, optional): Entity type filter ('all', 'vehicles', 'peds', 'objects')options
(table, optional): Additional filtering optionsexcludePlayerVehicle
(boolean): Exclude player's current vehicleonlyOccupied
(boolean): Only vehicles with driversonlyEmpty
(boolean): Only empty vehiclesplayersOnly
(boolean): Only player pedsnpcsOnly
(boolean): Only NPC pedsexcludeDead
(boolean): Exclude dead pedsexcludeInVehicle
(boolean): Exclude peds in vehiclesmodelFilter
(number): Specific model hashexcludeFrozen
(boolean): Exclude frozen objectsonlyVisible
(boolean): Only visible objects
Returns: table - Array of entity information sorted by distance
Example:
-- Get all nearby entities within 25 units
local entities = B2Lib.Entities.getNearby(GetEntityCoords(PlayerPedId()), 25.0, 'all')
-- Get only vehicles within 30 units, excluding player's vehicle
local vehicles = B2Lib.Entities.getNearby(coords, 30.0, 'vehicles', {
excludePlayerVehicle = true,
onlyEmpty = true
})
getNearbyVehicles
Gets nearby vehicles with detailed vehicle-specific information.
B2Lib.Entities.getNearbyVehicles(coords, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromradius
(number, optional): Search radius in unitsoptions
(table, optional): Vehicle-specific filtering options
Returns: table - Array of vehicle information sorted by distance
entity
(number): Vehicle handletype
(string): Entity typecoords
(vector3): Vehicle coordinatesdistance
(number): Distance from search pointmodel
(number): Model hashmodelName
(string): Display nameplate
(string): License plateheading
(number): Vehicle headinghealth
(number): Current healthmaxHealth
(number): Maximum healthlocked
(boolean): Door lock statusengineRunning
(boolean): Engine statedriver
(number): Driver ped handle (0 if empty)passengers
(number): Number of passengersclass
(number): Vehicle classvelocity
(vector3): Current velocity
Example:
local vehicles = B2Lib.Entities.getNearbyVehicles(coords, 50.0, {
excludePlayerVehicle = true
})
for _, vehicle in ipairs(vehicles) do
if not vehicle.locked and vehicle.driver == 0 then
print(string.format("Found unlocked %s at %.1fm", vehicle.modelName, vehicle.distance))
end
end
getNearbyPeds
Gets nearby peds with detailed ped-specific information.
B2Lib.Entities.getNearbyPeds(coords, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromradius
(number, optional): Search radius in unitsoptions
(table, optional): Ped-specific filtering options
Returns: table - Array of ped information sorted by distance
entity
(number): Ped handletype
(string): Entity typecoords
(vector3): Ped coordinatesdistance
(number): Distance from search pointmodel
(number): Model hashheading
(number): Ped headinghealth
(number): Current healthmaxHealth
(number): Maximum healthisPlayer
(boolean): Is this a player pedisDead
(boolean): Is ped deadisInVehicle
(boolean): Is ped in a vehicleweapon
(number): Current weapon hashvelocity
(vector3): Current velocityisAiming
(boolean): Is ped aimingisShooting
(boolean): Is ped shootingserverId
(number): Server ID (players only)playerName
(string): Player name (players only)
Example:
local nearbyPlayers = B2Lib.Entities.getNearbyPeds(coords, 10.0, {
playersOnly = true,
excludeDead = true
})
for _, player in ipairs(nearbyPlayers) do
print(string.format("Player %s (ID: %d) is %.1fm away",
player.playerName, player.serverId, player.distance))
end
getNearbyObjects
Gets nearby objects with detailed object-specific information.
B2Lib.Entities.getNearbyObjects(coords, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromradius
(number, optional): Search radius in unitsoptions
(table, optional): Object-specific filtering options
Returns: table - Array of object information sorted by distance
entity
(number): Object handletype
(string): Entity typecoords
(vector3): Object coordinatesdistance
(number): Distance from search pointmodel
(number): Model hashheading
(number): Object headingisFrozen
(boolean): Is object frozenisVisible
(boolean): Is object visiblehasPhysics
(boolean): Has physics enabledisAttached
(boolean): Is attached to another entityvelocity
(vector3): Current velocity
getClosest
Gets the closest entity of a specific type.
B2Lib.Entities.getClosest(coords, entityType, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromentityType
(string): Entity type ('vehicle', 'ped', 'object')radius
(number, optional): Search radius in unitsoptions
(table, optional): Filtering options
Returns: table|nil - Closest entity information or nil if none found
Example:
local closestVehicle = B2Lib.Entities.getClosest(coords, 'vehicle', 10.0)
if closestVehicle then
print("Closest vehicle:", closestVehicle.modelName)
end
getClosestVehicle
Gets the closest vehicle to specified coordinates.
B2Lib.Entities.getClosestVehicle(coords, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromradius
(number, optional): Search radius in unitsoptions
(table, optional): Vehicle filtering options
Returns: table|nil - Closest vehicle information or nil if none found
getClosestPed
Gets the closest ped to specified coordinates.
B2Lib.Entities.getClosestPed(coords, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromradius
(number, optional): Search radius in unitsoptions
(table, optional): Ped filtering options
Returns: table|nil - Closest ped information or nil if none found
getClosestObject
Gets the closest object to specified coordinates.
B2Lib.Entities.getClosestObject(coords, radius, options)
Parameters:
coords
(vector3, optional): Coordinates to search fromradius
(number, optional): Search radius in unitsoptions
(table, optional): Object filtering options
Returns: table|nil - Closest object information or nil if none found
getEntityInfo
Gets detailed information about a specific entity.
B2Lib.Entities.getEntityInfo(entity)
Parameters:
entity
(number): Entity handle
Returns: table|nil - Entity information or nil if invalid
Example:
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
if vehicle ~= 0 then
local info = B2Lib.Entities.getEntityInfo(vehicle)
print("Vehicle model:", info.modelName)
print("Plate:", info.plate)
end
isEntityValid
Checks if an entity handle is valid and exists.
B2Lib.Entities.isEntityValid(entity)
Parameters:
entity
(number): Entity handle to check
Returns: boolean - True if entity is valid, false otherwise
getEntityDistance
Gets the distance between two entities or an entity and coordinates.
B2Lib.Entities.getEntityDistance(entity1, entity2OrCoords)
Parameters:
entity1
(number): First entity handleentity2OrCoords
(number|vector3): Second entity handle or coordinates
Returns: number - Distance between entities/coordinates
getEntitiesInArea
Gets all entities within a specified area (box or sphere).
B2Lib.Entities.getEntitiesInArea(coords, size, shape, entityType)
Parameters:
coords
(vector3): Center coordinatessize
(number|vector3): Radius for sphere or dimensions for boxshape
(string, optional): 'sphere' or 'box' (default: 'sphere')entityType
(string, optional): Entity type filter
Returns: table - Array of entities in the area
filterEntities
Filters an array of entities based on specified criteria.
B2Lib.Entities.filterEntities(entities, filterOptions)
Parameters:
entities
(table): Array of entity informationfilterOptions
(table): Filtering criteria
Returns: table - Filtered array of entities
sortEntitiesByDistance
Sorts entities by distance from specified coordinates.
B2Lib.Entities.sortEntitiesByDistance(entities, coords)
Parameters:
entities
(table): Array of entity informationcoords
(vector3): Reference coordinates
Returns: table - Sorted array of entities
clearCache
Clears the entity information cache.
B2Lib.Entities.clearCache()
Returns: boolean - True if cache was cleared
getStats
Gets entity system statistics.
B2Lib.Entities.getStats()
Returns: table - Statistics about entity operations
cacheSize
(number): Current cache sizecacheHits
(number): Cache hit countcacheMisses
(number): Cache miss counttotalQueries
(number): Total entity queriesaverageQueryTime
(number): Average query time in ms
Configuration
Configure the entities system in config.lua
:
Config.Entities = {
defaultRadius = 50.0, -- Default search radius
maxCacheSize = 500, -- Maximum cache entries
cacheTimeout = 5000, -- Cache timeout in milliseconds
enableCache = true, -- Enable entity caching
enableStats = true, -- Enable statistics tracking
debugMode = false -- Enable debug logging
}
Troubleshooting
No Entities Found
Check that the search radius is appropriate
Verify coordinates are valid
Ensure entity type filter is correct
Check if entities exist in the area
Performance Issues
Reduce search radius for better performance
Use specific entity type filters instead of 'all'
Enable caching to reduce repeated queries
Use filtering options to limit results
Outdated Entity Information
Clear cache with
clearCache()
if neededReduce cache timeout for more frequent updates
Check if entities have been deleted or moved
Verify entity handles are still valid
Memory Usage
Reduce maxCacheSize in configuration
Clear cache periodically in long-running scripts
Use specific queries instead of broad searches
Monitor cache statistics with
getStats()
Last updated