Vehicles

The Vehicles module provides comprehensive vehicle functionality for FiveM resources with advanced customization, tracking, and management capabilities.

setTuning

Applies comprehensive tuning modifications to a vehicle.

B2Lib.Vehicles.setTuning(vehicle, tuning)

Parameters:

  • vehicle (number): Vehicle entity handle

  • tuning (table): Tuning configuration

    • engine (number): Engine upgrade level (0-3)

    • brakes (number): Brake upgrade level (0-3)

    • transmission (number): Transmission upgrade level (0-3)

    • suspension (number): Suspension upgrade level (0-3)

    • armor (number): Armor upgrade level (0-4)

    • turbo (boolean): Enable turbo

    • spoiler (number): Spoiler modification

    • frontBumper (number): Front bumper modification

    • rearBumper (number): Rear bumper modification

    • sideSkirt (number): Side skirt modification

    • exhaust (number): Exhaust modification

    • frame (number): Frame modification

    • grille (number): Grille modification

    • hood (number): Hood modification

    • fender (number): Fender modification

    • rightFender (number): Right fender modification

    • roof (number): Roof modification

    • xenonHeadlights (boolean): Enable xenon headlights

    • xenonColor (number): Xenon color (0-12)

    • wheelType (number): Wheel type category

    • frontWheels (number): Front wheel design

    • backWheels (number): Back wheel design

    • livery (number): Primary livery

    • livery2 (number): Secondary livery

    • vanityPlate (number): Vanity plate style

    • trimA (number): Trim A modification

    • trimB (number): Trim B modification

Returns: boolean - True if tuning was applied successfully

Example:

local tuning = {
    engine = 3,
    brakes = 2,
    transmission = 1,
    turbo = true,
    spoiler = 1,
    livery = 2
}
local success = B2Lib.Vehicles.setTuning(vehicle, tuning)

getTuning

Gets the current tuning configuration of a vehicle.

B2Lib.Vehicles.getTuning(vehicle)

Parameters:

  • vehicle (number): Vehicle entity handle

Returns: table - Current tuning configuration with all modification levels

setColors

Applies color configuration to a vehicle including primary, secondary, custom RGB, and neon colors.

B2Lib.Vehicles.setColors(vehicle, colors)

Parameters:

  • vehicle (number): Vehicle entity handle

  • colors (table): Color configuration

    • primary (number): Primary color index

    • secondary (number): Secondary color index

    • pearl (number): Pearl color index

    • wheel (number): Wheel color index

    • customPrimary (table): Custom primary RGB {r, g, b}

    • customSecondary (table): Custom secondary RGB {r, g, b}

    • neon (table): Neon RGB color {r, g, b}

    • neonSides (table): Neon sides enabled {left, right, front, back}

    • interior (number): Interior color index

    • dashboard (number): Dashboard color index

    • tyreSmokeColor (table): Tire smoke RGB {r, g, b}

Returns: boolean - True if colors were applied successfully

Example:

local colors = {
    primary = 12,
    secondary = 0,
    customPrimary = {r = 255, g = 0, b = 0},
    neon = {r = 0, g = 255, b = 255},
    neonSides = {true, true, true, true}
}
B2Lib.Vehicles.setColors(vehicle, colors)

getColors

Gets the current color configuration of a vehicle.

B2Lib.Vehicles.getColors(vehicle)

Parameters:

  • vehicle (number): Vehicle entity handle

Returns: table - Current color configuration including all color types

setExtras

Configures vehicle extras (additional components).

B2Lib.Vehicles.setExtras(vehicle, extras)

Parameters:

  • vehicle (number): Vehicle entity handle

  • extras (table): Extras configuration {[extraId] = enabled}

Returns: boolean - True if extras were configured successfully

Example:

local extras = {
    [1] = true,   -- Enable extra 1
    [2] = false,  -- Disable extra 2
    [3] = true    -- Enable extra 3
}
B2Lib.Vehicles.setExtras(vehicle, extras)

getExtras

Gets information about available and enabled vehicle extras.

B2Lib.Vehicles.getExtras(vehicle)

Parameters:

  • vehicle (number): Vehicle entity handle

Returns: table - Extras information

  • available (table): Array of available extra IDs

  • extras (table): Current extra states {[extraId] = enabled}

  • count (number): Total number of available extras

getProperties

Gets comprehensive vehicle properties including all modifications and states.

B2Lib.Vehicles.getProperties(vehicle)

Parameters:

  • vehicle (number): Vehicle entity handle

Returns: table - Complete vehicle properties

  • model (number): Vehicle model hash

  • plate (string): License plate text

  • plateIndex (number): License plate style

  • bodyHealth (number): Body health (0-1000)

  • engineHealth (number): Engine health (0-1000)

  • tankHealth (number): Fuel tank health (0-1000)

  • fuelLevel (number): Fuel level (0-100)

  • dirtLevel (number): Dirt level (0-15)

  • oilLevel (number): Oil level (0-100)

  • tuning (table): All tuning modifications

  • colors (table): All color configurations

  • extras (table): Extra states

  • doors (table): Door states

  • windows (table): Window states

  • tyres (table): Tire states

  • neonEnabled (table): Neon light states

setProperties

Applies comprehensive vehicle properties to restore or configure a vehicle.

B2Lib.Vehicles.setProperties(vehicle, properties)

Parameters:

  • vehicle (number): Vehicle entity handle

  • properties (table): Vehicle properties to apply

Returns: boolean - True if properties were applied successfully

spawn

Spawns a vehicle with optional customization properties.

B2Lib.Vehicles.spawn(model, coords, heading, properties)

Parameters:

  • model (string|number): Vehicle model name or hash

  • coords (vector3): Spawn coordinates

  • heading (number): Vehicle heading in degrees

  • properties (table, optional): Vehicle properties to apply after spawn

Returns: number|nil - Vehicle entity handle or nil if spawn failed

Example:

local vehicle = B2Lib.Vehicles.spawn('adder', vector3(0, 0, 72), 0.0, {
    colors = {primary = 12, secondary = 0},
    tuning = {engine = 3, turbo = true}
})

trackHealth

Starts tracking vehicle health changes with callback notifications.

B2Lib.Vehicles.trackHealth(vehicle, callback, options)

Parameters:

  • vehicle (number): Vehicle entity handle

  • callback (function): Function called on health changes

  • options (table, optional): Tracking options

    • interval (number): Check interval in milliseconds (default: 1000)

    • threshold (number): Minimum change threshold (default: 10)

    • trackDamage (boolean): Track individual damage components (default: true)

Returns: string - Tracking ID for management

Example:

local trackingId = B2Lib.Vehicles.trackHealth(vehicle, function(health, damage)
    print("Vehicle health:", health.body, health.engine)
    if damage.newDamage then
        print("New damage detected!")
    end
end)

stopTracking

Stops health tracking for a vehicle.

B2Lib.Vehicles.stopTracking(trackingId)

Parameters:

  • trackingId (string): Tracking ID to stop

Returns: boolean - True if tracking was stopped successfully

getTrackingInfo

Gets information about active health tracking.

B2Lib.Vehicles.getTrackingInfo(trackingId)

Parameters:

  • trackingId (string): Tracking ID to get information for

Returns: table|nil - Tracking information or nil if not found

repair

Repairs vehicle damage with configurable options.

B2Lib.Vehicles.repair(vehicle, options)

Parameters:

  • vehicle (number): Vehicle entity handle

  • options (table, optional): Repair options

    • body (boolean): Repair body damage (default: true)

    • engine (boolean): Repair engine damage (default: true)

    • tank (boolean): Repair fuel tank (default: true)

    • visual (boolean): Repair visual damage (default: true)

    • deformation (boolean): Fix deformation (default: true)

    • doors (boolean): Repair doors (default: true)

    • windows (boolean): Repair windows (default: true)

    • tyres (boolean): Repair tires (default: true)

    • lights (boolean): Repair lights (default: true)

Returns: boolean - True if repair was successful

getVehicleType

Gets detailed vehicle type information and classification.

B2Lib.Vehicles.getVehicleType(vehicle)

Parameters:

  • vehicle (number): Vehicle entity handle

Returns: table - Vehicle type information

  • class (number): Vehicle class ID

  • className (string): Vehicle class name

  • type (string): Vehicle type (car, bike, boat, plane, helicopter, etc.)

  • category (string): Vehicle category

  • capabilities (table): Vehicle capabilities

getClassName

Gets the vehicle class name.

B2Lib.Vehicles.getClassName(vehicle)

Parameters:

  • vehicle (number): Vehicle entity handle

Returns: string - Vehicle class name

hasCapability

Checks if a vehicle has a specific capability.

B2Lib.Vehicles.hasCapability(vehicle, capability)

Parameters:

  • vehicle (number): Vehicle entity handle

  • capability (string): Capability to check (neon, siren, flight, etc.)

Returns: boolean - True if vehicle has the capability

getMaxSpeed

Gets the maximum speed of a vehicle in specified units.

B2Lib.Vehicles.getMaxSpeed(vehicle, unit)

Parameters:

  • vehicle (number): Vehicle entity handle

  • unit (string, optional): Speed unit ('ms', 'kmh', 'mph') (default: 'ms')

Returns: number - Maximum speed in specified unit

getStats

Gets comprehensive vehicle system statistics.

B2Lib.Vehicles.getStats()

Returns: table - Vehicle system statistics

  • totalVehicles (number): Total vehicles managed

  • trackedVehicles (number): Vehicles being health tracked

  • spawnedVehicles (number): Vehicles spawned by system

  • totalRepairs (number): Total repairs performed

  • totalCustomizations (number): Total customizations applied

  • averageHealth (number): Average vehicle health

  • memoryUsage (number): Estimated memory usage

  • performanceMetrics (table): Performance statistics

Configuration

Configure the vehicles module in config.lua:

Config.Vehicles = {
    enableHealthTracking = true,    -- Enable health tracking system
    defaultTrackingInterval = 1000, -- Default tracking interval in ms
    healthThreshold = 10,           -- Minimum health change threshold
    enableStatistics = true,        -- Enable statistics tracking
    enableDebugMode = false,        -- Enable debug logging
    maxTrackedVehicles = 100,       -- Maximum vehicles to track simultaneously
    autoRepairThreshold = 100,      -- Auto-repair threshold
    enablePerformanceMode = false,  -- Enable performance optimizations
    cacheVehicleData = true,        -- Cache vehicle data for performance
    enableDamageDetection = true,   -- Enable damage detection
    trackingCleanupInterval = 300000, -- Cleanup interval for tracking
    maxSpawnDistance = 500,         -- Maximum spawn distance from players
    enableSpawnValidation = true,   -- Validate spawn locations
    defaultFuelLevel = 100,         -- Default fuel level for spawned vehicles
    enableCustomColors = true,      -- Enable custom RGB colors
    enableNeonLights = true,        -- Enable neon light system
    maxCustomizations = 50          -- Maximum customizations per vehicle
}

Troubleshooting

Vehicle Not Spawning

  1. Check if model name/hash is valid

  2. Verify spawn coordinates are valid and not obstructed

  3. Ensure player has necessary permissions

  4. Check if spawn distance limits are exceeded

Tuning Not Applying

  1. Verify vehicle supports the modification type

  2. Check if modification values are within valid ranges

  3. Ensure vehicle entity is valid and exists

  4. Validate tuning configuration structure

Health Tracking Issues

  1. Check if tracking ID is valid and active

  2. Verify callback function is properly defined

  3. Ensure tracking interval is reasonable

  4. Monitor for memory leaks with many tracked vehicles

Color Problems

  1. Verify color indices are within valid ranges (0-159)

  2. Check RGB values are between 0-255

  3. Ensure vehicle supports custom colors

  4. Validate neon light compatibility

Performance Issues

  1. Limit number of simultaneously tracked vehicles

  2. Increase tracking intervals for better performance

  3. Enable performance mode for large vehicle counts

  4. Use cleanup functions to remove old tracking data

Memory Leaks

  1. Always stop tracking when vehicles are deleted

  2. Use cleanup intervals to remove stale data

  3. Monitor statistics for memory usage trends

  4. Avoid creating too many simultaneous customizations

Last updated