Zones (PolyZones Alternative)
The Zones module provides comprehensive zone management for FiveM resources with support for multiple zone types, advanced collision detection, event handling, and performance optimization.
createPolygon
Creates a polygon zone with complex shape support and 2D collision detection.
B2Lib.Zones.createPolygon(options)Parameters:
options(table): Zone configuration optionspoints(table): Array of vector3 points defining polygon verticesminZ(number, optional): Minimum Z coordinate for zone boundsmaxZ(number, optional): Maximum Z coordinate for zone boundsname(string, optional): Custom zone identifieronEnter(function, optional): Callback when player enters zoneonExit(function, optional): Callback when player exits zoneonInside(function, optional): Callback while player is inside zonedata(table, optional): Custom data attached to zoneactive(boolean, optional): Whether zone is active (default: true)debug(boolean, optional): Enable debug renderingpriority(number, optional): Zone priority for processing ordertags(table, optional): Array of tags for zone categorization
Returns: string - Unique zone identifier
Example:
createCircle
Creates a circle zone with 2D collision detection and configurable radius.
Parameters:
options(table): Zone configuration optionscenter(vector3): Center coordinates of the circleradius(number): Circle radius in unitsminZ(number, optional): Minimum Z coordinate (default: center.z - 100)maxZ(number, optional): Maximum Z coordinate (default: center.z + 100)name(string, optional): Custom zone identifieronEnter(function, optional): Callback when player enters zoneonExit(function, optional): Callback when player exits zoneonInside(function, optional): Callback while player is inside zonedata(table, optional): Custom data attached to zoneactive(boolean, optional): Whether zone is active (default: true)debug(boolean, optional): Enable debug renderingpriority(number, optional): Zone priority for processing ordertags(table, optional): Array of tags for zone categorization
Returns: string - Unique zone identifier
Example:
createSphere
Creates a sphere zone with 3D collision detection for full spatial coverage.
Parameters:
options(table): Zone configuration optionscenter(vector3): Center coordinates of the sphereradius(number): Sphere radius in unitsname(string, optional): Custom zone identifieronEnter(function, optional): Callback when player enters zoneonExit(function, optional): Callback when player exits zoneonInside(function, optional): Callback while player is inside zonedata(table, optional): Custom data attached to zoneactive(boolean, optional): Whether zone is active (default: true)debug(boolean, optional): Enable debug renderingpriority(number, optional): Zone priority for processing ordertags(table, optional): Array of tags for zone categorization
Returns: string - Unique zone identifier
Example:
createBox
Creates a box zone with optional rotation support and 3D collision detection.
Parameters:
options(table): Zone configuration optionscenter(vector3): Center coordinates of the boxsize(vector3): Box dimensions (x, y, z)heading(number, optional): Box rotation in degrees (default: 0)name(string, optional): Custom zone identifieronEnter(function, optional): Callback when player enters zoneonExit(function, optional): Callback when player exits zoneonInside(function, optional): Callback while player is inside zonedata(table, optional): Custom data attached to zoneactive(boolean, optional): Whether zone is active (default: true)debug(boolean, optional): Enable debug renderingpriority(number, optional): Zone priority for processing ordertags(table, optional): Array of tags for zone categorization
Returns: string - Unique zone identifier
Example:
isPointInside
Checks if a point is inside a specific zone with optimized collision detection.
Parameters:
zoneId(string): Zone identifier to checkpoint(vector3): Point coordinates to test
Returns: boolean - True if point is inside the zone
Example:
getZone
Retrieves zone information by ID with complete zone data.
Parameters:
zoneId(string): Zone identifier
Returns: table|nil - Zone object or nil if not found
id(string): Zone identifiertype(string): Zone type (polygon, circle, sphere, box)active(boolean): Whether zone is activedata(table): Custom zone dataenterCount(number): Number of times zone was enteredexitCount(number): Number of times zone was exitedlastEnter(number): Timestamp of last entrylastExit(number): Timestamp of last exitcreatedAt(number): Zone creation timestamp
getAllZones
Retrieves all zones with optional filtering capabilities.
Parameters:
filterOptions(table, optional): Filtering criteriaactive(boolean): Filter by active statetype(string): Filter by zone typetags(table): Filter by tagshasData(string): Filter zones with specific data key
Returns: table - Array of zone objects
Example:
remove
Removes a zone from the system with proper cleanup.
Parameters:
zoneId(string): Zone identifier to remove
Returns: boolean - True if zone was successfully removed
Example:
setActive
Enables or disables a zone without removing it.
Parameters:
zoneId(string): Zone identifieractive(boolean): New active state
Returns: boolean - True if zone state was successfully changed
Example:
updateData
Updates zone data properties with merge functionality.
Parameters:
zoneId(string): Zone identifierdata(table): New data to merge with existing data
Returns: boolean - True if data was successfully updated
Example:
modifyZone
Modifies zone geometry including position, size, and rotation.
Parameters:
zoneId(string): Zone identifiermodifications(table): Geometry modificationscenter(vector3): New center positionradius(number): New radius (for circles/spheres)size(vector3): New size (for boxes)heading(number): New rotation (for boxes)points(table): New points (for polygons)minZ(number): New minimum Z coordinatemaxZ(number): New maximum Z coordinate
Returns: boolean - True if zone was successfully modified
Example:
getCurrentZones
Gets zones that the player is currently inside.
Returns: table - Array of zone IDs the player is currently in
Example:
getZoneHistory
Gets zone entry/exit history for analysis and debugging.
Parameters:
zoneId(string): Zone identifierlimit(number, optional): Maximum number of history entries to return
Returns: table - Array of history entries
type(string): Event type ("enter" or "exit")timestamp(number): Event timestampcoordinates(vector3): Player coordinates at event timereason(string, optional): Reason for event (e.g., "zone_removed")
Example:
getStats
Gets comprehensive zone system statistics and performance metrics.
Returns: table - Zone system statistics
totalZones(number): Total number of zonesactiveZones(number): Number of active zonescurrentPlayerZones(number): Zones player is currently intotalEnters(number): Total zone entriestotalExits(number): Total zone exitssystemStats(table): Performance statisticstotalChecks(number): Total zone checks performedaverageCheckTime(number): Average check time in millisecondslastCheckTime(number): Last check time in milliseconds
config(table): Current system configuration
startChecking
Starts the zone checking system for automatic zone monitoring.
Example:
stopChecking
Stops the zone checking system to pause monitoring.
Example:
setCheckInterval
Sets the zone check interval for performance optimization.
Parameters:
interval(number): Interval in milliseconds (minimum 50ms)
Example:
getCheckInterval
Gets the current zone check interval.
Returns: number - Current interval in milliseconds
setDebug
Enables or disables debug rendering for a specific zone.
Parameters:
zoneId(string): Zone identifierenabled(boolean): Debug rendering state
Example:
updateConfig
Updates zone system configuration with new settings.
Parameters:
newConfig(table): Configuration options to update
Example:
Configuration
Configure the zones system in config.lua:
Advanced Examples
Multi-Zone Event System
Dynamic Zone Management
Zone-Based Gameplay Systems
Troubleshooting
Zones Not Triggering
Check that zone checking system is started with
startChecking()Verify zone is active with
getZone()and checkactivepropertyEnsure callback functions don't throw errors
Check zone coordinates and player position are valid
Performance Issues
Reduce check interval with
setCheckInterval()for better performanceLimit number of active zones (use
setActive()to disable unused zones)Use appropriate zone types (circles are faster than polygons)
Enable spatial optimization in configuration
Zone Overlap Problems
Use zone priorities to control processing order
Check
getCurrentZones()to see all active zonesDesign zones to minimize unnecessary overlaps
Use zone tags for better organization
Memory Leaks
Always remove zones when no longer needed
Limit zone history with
maxHistoryEntriesconfigurationMonitor statistics with
getStats()for memory usageClean up event handlers in zone callbacks
Debug and Visualization
Enable debug mode in configuration for detailed logging
Use
setDebug()for individual zone visualizationCheck zone history with
getZoneHistory()for event trackingMonitor performance with statistics from
getStats()
Last updated