NightZoom LogoNightZoom
BJJ Racing Core/BJJ Racing Core v7/Vehicle

Vehicle Server

Server-side vehicle functionality and exports for BJJ Racing Core v7

This page covers server-side vehicle functionality for BJJ Racing Core v7. These functions handle vehicle database operations, spawning, live monitoring, and object-oriented vehicle management.

Core Access

Access the core system and vehicle functions:

local Core = exports.bjj_racingcore:getCore()

Vehicle Management

Core.Vehicle.generatePlate()

Generate a random license plate.

local plate = Core.Vehicle.generatePlate()

Returns: string - Random license plate (e.g., "AB1234")

Core.Vehicle.generateVin()

Generate a random 17-character VIN.

local vin = Core.Vehicle.generateVin()

Returns: string - 17-character VIN string

Core.Vehicle.createVehicle(props)

Create a new vehicle in the database.

local vehicle = Core.Vehicle.createVehicle(vehicleData)

Parameters:

  • props: table - Vehicle properties
    • model: string - Vehicle model name
    • class: string - Vehicle class
    • owner: number - Character ID of owner
    • fuel: number - Initial fuel level (0-100)
    • nos: number - Initial NOS level
    • plate: string - License plate (optional, auto-generated)
    • vin: string - VIN (optional, auto-generated)

Returns: Vehicle|nil - Vehicle object instance or nil if failed

Example:

local vehicleData = {
    model = "adder",
    class = "super",
    owner = charId,
    fuel = 100,
    nos = 0
}
local vehicle = Core.Vehicle.createVehicle(vehicleData)

Core.Vehicle.getVehicleById(vehicleId)

Get vehicle data by database ID.

local vehicleData = Core.Vehicle.getVehicleById(vehicleId)

Parameters:

  • vehicleId: string - Vehicle database ID

Returns: table|nil - Vehicle data or nil if not found

Core.Vehicle.getPlayerVehicles(charId)

Get all vehicles owned by a character.

local vehicles = Core.Vehicle.getPlayerVehicles(charId)

Parameters:

  • charId: number - Character ID

Returns: table[] - Array of vehicle data objects

Core.Vehicle.takeOutVehicle(source, vehicleId, charId, doWarp)

Spawn a vehicle for a player.

local success, data = Core.Vehicle.takeOutVehicle(source, vehicleId, charId, doWarp)

Parameters:

  • source: number - Player source ID
  • vehicleId: string - Vehicle database ID
  • charId: number - Character ID
  • doWarp: boolean - Whether to warp player into vehicle (optional)

Returns: number|boolean, string - NetworkID on success, false and error message on failure

Example:

local networkId, vehicleData = Core.Vehicle.takeOutVehicle(source, vehicleId, charId, true)
if networkId then
    print("Vehicle spawned with NetworkID:", networkId)
else
    print("Error:", vehicleData)
end

Core.Vehicle.saveVehicle(vehicleId, props)

Save vehicle properties to database.

local success = Core.Vehicle.saveVehicle(vehicleId, props)

Parameters:

  • vehicleId: string - Vehicle database ID
  • props: table - Properties to update

Returns: boolean - Success status

Core.Vehicle.getVehicleState(networkId)

Get vehicle entity state.

local state = Core.Vehicle.getVehicleState(networkId)

Parameters:

  • networkId: number - Vehicle network ID

Returns: table|nil - Entity state or nil if not found

Vehicle Class System

Core.VehicleClass.getById(vehicleId)

Get a vehicle object instance by ID.

local vehicle = Core.VehicleClass.getById(vehicleId)

Parameters:

  • vehicleId: string - Vehicle database ID

Returns: Vehicle|nil - Vehicle object with methods

Core.VehicleClass.create(props)

Create new vehicle in database and return object.

local vehicle = Core.VehicleClass.create(props)

Parameters:

  • props: table - Vehicle properties (same as createVehicle)

Returns: Vehicle|nil - Vehicle object instance

Core.VehicleClass.getByOwner(charId)

Get vehicle objects owned by character.

local vehicles = Core.VehicleClass.getByOwner(charId)

Parameters:

  • charId: number - Character ID

Returns: Vehicle[] - Array of vehicle objects

Vehicle Object Methods

Once you have a vehicle object, you can use these methods:

Data Access Methods

vehicle.getData(key)

Get a data property value.

local value = vehicle.getData('fuel')

vehicle.setData(key, value, autoSave)

Set a data property value.

local success = vehicle.setData('fuel', 75, true)

vehicle.getPlate()

Get vehicle license plate.

local plate = vehicle.getPlate()

vehicle.getOwner()

Get vehicle owner character ID.

local charId = vehicle.getOwner()

vehicle.getFuel()

Get vehicle fuel level.

local fuel = vehicle.getFuel()  -- 0-100

vehicle.getNos()

Get vehicle NOS level.

local nos = vehicle.getNos()

Update Methods

vehicle.setPlate(plate, autoSave)

Set vehicle license plate.

local success = vehicle.setPlate("ABC123", true)

vehicle.setFuel(amount, autoSave)

Set vehicle fuel level.

local success = vehicle.setFuel(50, true)  -- Set to 50%

vehicle.addFuel(amount, autoSave)

Add fuel to vehicle.

local success = vehicle.addFuel(10, true)  -- Add 10%

vehicle.removeFuel(amount, autoSave)

Remove fuel from vehicle.

local success = vehicle.removeFuel(5, false)  -- Remove 5%, don't save

vehicle.setNos(amount, autoSave)

Set vehicle NOS level.

local success = vehicle.setNos(75, true)

vehicle.addNos(amount, autoSave)

Add NOS to vehicle.

local success = vehicle.addNos(25, true)

vehicle.removeNos(amount, autoSave)

Remove NOS from vehicle.

local success = vehicle.removeNos(10, false)

Damage Management

vehicle.getEngineHealth()

Get engine health (0-1000).

local health = vehicle.getEngineHealth()

vehicle.getBodyHealth()

Get body health (0-1000).

local health = vehicle.getBodyHealth()

vehicle.setEngineHealth(health, autoSave)

Set engine health.

local success = vehicle.setEngineHealth(750, true)

vehicle.setBodyHealth(health, autoSave)

Set body health.

local success = vehicle.setBodyHealth(850, true)

vehicle.setDamage(engineHealth, bodyHealth, autoSave)

Set both engine and body health.

local success = vehicle.setDamage(600, 700, true)

vehicle.isEngineDestroyed()

Check if engine is destroyed.

local isDestroyed = vehicle.isEngineDestroyed()  -- true if health <= 100

vehicle.isSeverelyDamaged()

Check if vehicle is severely damaged.

local isSevere = vehicle.isSeverelyDamaged()  -- true if health < 300

Spawning & Entity Management

vehicle.spawn(source, coords, heading, doWarp)

Spawn the vehicle in the world.

local networkId = vehicle.spawn(source, coords, heading, doWarp)

Parameters:

  • source: number - Player source ID
  • coords: vector3 - Spawn coordinates (optional, uses player location)
  • heading: number - Vehicle heading (optional)
  • doWarp: boolean - Warp player into vehicle (optional)

Returns: number|boolean - NetworkID on success, false on failure

vehicle.despawn()

Remove vehicle from world.

local success = vehicle.despawn()

vehicle.isSpawned()

Check if vehicle is currently spawned.

local isActive = vehicle.isSpawned()

vehicle.getNetworkId()

Get network ID if spawned.

local networkId = vehicle.getNetworkId()

Ownership Management

vehicle.isOwnedBy(charId)

Check if character owns vehicle.

local isOwner = vehicle.isOwnedBy(charId)

vehicle.transferOwnership(newCharId, autoSave)

Transfer vehicle ownership.

local success = vehicle.transferOwnership(newCharId, true)

Database Operations

vehicle.save()

Save vehicle to database.

vehicle.save()

vehicle.delete()

Delete vehicle from database.

vehicle.delete()

Live System Manager

Vehicle Live Manager Access

local VehicleLiveManager = exports.bjj_racingcore:getVehicleLiveManager()

VehicleLiveManager.registerVehicle(networkId, vehicleObj)

Register vehicle for live monitoring.

VehicleLiveManager.registerVehicle(networkId, vehicleObj)

VehicleLiveManager.getActiveVehicle(networkId)

Get active vehicle object.

local vehicle = VehicleLiveManager.getActiveVehicle(networkId)

VehicleLiveManager.getVehicleDamage(networkId)

Get detailed damage information.

local damageInfo = VehicleLiveManager.getVehicleDamage(networkId)
if damageInfo then
    print("Engine Health:", damageInfo.engineHealth)
    print("Body Health:", damageInfo.bodyHealth)
    print("Is Destroyed:", damageInfo.isEngineDestroyed)
end

Vehicle Events

Receiving Events

EventParametersDescription
bjj_racingcore:server:vehicleSpawnednetworkId: number, vehicleData: table, source: numberVehicle has been spawned
bjj_racingcore:server:syncVehicleStatenetworkId: number, property: string, value: anyClient syncing vehicle state
bjj_racingcore:server:syncVehicleDamagenetworkId: number, engineHealth: number, bodyHealth: numberClient syncing damage

Event Handlers

-- Vehicle spawned event
RegisterNetEvent('bjj_racingcore:server:vehicleSpawned', function(networkId, vehicleData, source)
    print("Vehicle spawned:", vehicleData.model, "for player:", source)
    
    -- Vehicle is automatically registered for live monitoring
end)

-- State synchronization
RegisterNetEvent('bjj_racingcore:server:syncVehicleState', function(networkId, property, value)
    -- Validates ownership and updates vehicle object
    -- Properties: 'fuel', 'nos', 'mods', 'engineState', 'isCharging', 'isDead'
end)

-- Damage synchronization
RegisterNetEvent('bjj_racingcore:server:syncVehicleDamage', function(networkId, engineHealth, bodyHealth)
    -- Validates ownership and updates damage values
    -- Auto-saves if damage is significant (< 500 health)
end)

Callback Functions

bjj_racingcore:server:spawnVehicleById

Spawn vehicle by ID with automatic ownership validation.

lib.callback('bjj_racingcore:server:spawnVehicleById', vehicleId, function(networkId, vehicleData)
    if networkId then
        print("Vehicle spawned successfully")
    else
        print("Failed to spawn vehicle:", vehicleData)
    end
end)

Live System Hooks

Register custom hooks for vehicle events:

local vehicle = Core.VehicleClass.getById(vehicleId)

-- Register fuel system hook
vehicle.registerLiveSystemHook("fuel", function(vehicleObj, property, oldValue, newValue)
    if newValue < 10 and oldValue >= 10 then
        -- Trigger low fuel warning
        local owner = vehicleObj.getOwner()
        local player = Core.getPlayerByCharId(owner)
        if player then
            TriggerClientEvent('vehicle:lowFuelWarning', player.source, newValue)
        end
    end
end)

-- Register damage hook
vehicle.registerLiveSystemHook("engineDamage", function(vehicleObj, property, oldHealth, newHealth)
    if newHealth < 300 then
        print("Critical engine damage on vehicle:", vehicleObj.id)
    end
end)

Integration Example

-- Example: Vehicle rental system
AddEventHandler('rental:vehicleRequested', function(source, vehicleModel)
    local player = Core.getPlayer(source)
    if not player then return end
    
    -- Create rental vehicle
    local vehicle = Core.Vehicle.createVehicle({
        model = vehicleModel,
        class = "rental",
        owner = player.data.charId,
        fuel = 100,
        nos = 0
    })
    
    if vehicle then
        -- Spawn vehicle
        local networkId = vehicle.spawn(source, nil, nil, true)
        
        if networkId then
            -- Set rental metadata
            vehicle.setData("isRental", true, false)
            vehicle.setData("rentalExpiry", os.time() + 3600, false) -- 1 hour
            vehicle.save()
            
            -- Register for live monitoring
            local VehicleLiveManager = exports.bjj_racingcore:getVehicleLiveManager()
            VehicleLiveManager.registerVehicle(networkId, vehicle)
            
            print("Rental vehicle created for player:", player.data.name)
        end
    end
end)

The vehicle server system automatically handles persistence, live monitoring, and integration with subsystems. All vehicle data is automatically saved based on activity levels and change thresholds.

Vehicle state synchronization includes ownership validation to prevent unauthorized modifications. Only the vehicle's driver can sync state changes from the client.

On this page

Core AccessVehicle ManagementCore.Vehicle.generatePlate()Core.Vehicle.generateVin()Core.Vehicle.createVehicle(props)Core.Vehicle.getVehicleById(vehicleId)Core.Vehicle.getPlayerVehicles(charId)Core.Vehicle.takeOutVehicle(source, vehicleId, charId, doWarp)Core.Vehicle.saveVehicle(vehicleId, props)Core.Vehicle.getVehicleState(networkId)Vehicle Class SystemCore.VehicleClass.getById(vehicleId)Core.VehicleClass.create(props)Core.VehicleClass.getByOwner(charId)Vehicle Object MethodsData Access Methodsvehicle.getData(key)vehicle.setData(key, value, autoSave)vehicle.getPlate()vehicle.getOwner()vehicle.getFuel()vehicle.getNos()Update Methodsvehicle.setPlate(plate, autoSave)vehicle.setFuel(amount, autoSave)vehicle.addFuel(amount, autoSave)vehicle.removeFuel(amount, autoSave)vehicle.setNos(amount, autoSave)vehicle.addNos(amount, autoSave)vehicle.removeNos(amount, autoSave)Damage Managementvehicle.getEngineHealth()vehicle.getBodyHealth()vehicle.setEngineHealth(health, autoSave)vehicle.setBodyHealth(health, autoSave)vehicle.setDamage(engineHealth, bodyHealth, autoSave)vehicle.isEngineDestroyed()vehicle.isSeverelyDamaged()Spawning & Entity Managementvehicle.spawn(source, coords, heading, doWarp)vehicle.despawn()vehicle.isSpawned()vehicle.getNetworkId()Ownership Managementvehicle.isOwnedBy(charId)vehicle.transferOwnership(newCharId, autoSave)Database Operationsvehicle.save()vehicle.delete()Live System ManagerVehicle Live Manager AccessVehicleLiveManager.registerVehicle(networkId, vehicleObj)VehicleLiveManager.getActiveVehicle(networkId)VehicleLiveManager.getVehicleDamage(networkId)Vehicle EventsReceiving EventsEvent HandlersCallback Functionsbjj_racingcore:server:spawnVehicleByIdLive System HooksIntegration Example