NightZoom LogoNightZoom
BJJ Racing Core/BJJ Racing Core v7

System States

Global and object entity states for BJJ Racing Core v7

This page covers global server states and object entity states for BJJ Racing Core v7. Global states are synchronized server-wide to all clients, while object states track non-vehicle, non-player entities.

Global States

Global states are server-wide states synchronized to all clients for system-wide functionality.

Time & Weather System

StateTypeDescription
currentTimetableCurrent in-game time
freezeTimebooleanTime progression is frozen
timeScalenumberTime progression speed multiplier
weathertableCurrent weather state
blackOutbooleanServer-wide blackout state

Content & Streaming

StateTypeDescription
streamermodebooleanServer-wide streamer mode

Localization & Configuration

StateTypeDescription
ox_lib:localestableAvailable localization data
group.<groupName>tableGroup configuration data

Usage Examples

Time System Integration

-- Access current server time
local currentTime = GlobalState.currentTime
if currentTime then
    print("Current server time:", json.encode(currentTime))
    
    -- Example time structure: {hour: 12, minute: 30}
    local hour = currentTime.hour
    local minute = currentTime.minute
    
    -- Update UI with current time
    SendNUIMessage({
        type = 'updateTime',
        hour = hour,
        minute = minute
    })
end

-- Check if time is frozen
local timeFrozen = GlobalState.freezeTime
if timeFrozen then
    print("Time is currently frozen")
    -- Disable time-based systems
    TriggerEvent('timesystem:disable')
else
    -- Enable time-based systems
    TriggerEvent('timesystem:enable')
end

-- Monitor time scale changes
AddStateBagChangeHandler('timeScale', 'global', function(_, _, value)
    local scale = value or 1.0
    print("Time scale changed to:", scale)
    
    -- Update time-sensitive systems
    TriggerEvent('timesystem:updateScale', scale)
end)

Weather System Integration

-- Monitor weather changes
AddStateBagChangeHandler('weather', 'global', function(_, _, value)
    if value then
        print("Weather changed:", json.encode(value))
        
        -- Example weather structure handling
        local weatherType = value.type or 'clear'
        local windSpeed = value.wind or 0.0
        local temperature = value.temperature or 20
        
        -- Update weather-dependent systems
        TriggerEvent('weather:changed', {
            type = weatherType,
            wind = windSpeed,
            temperature = temperature
        })
        
        -- Update UI
        SendNUIMessage({
            type = 'updateWeather',
            weather = weatherType,
            wind = windSpeed,
            temp = temperature
        })
    end
end)

-- Check for server-wide blackout
local isBlackout = GlobalState.blackOut
if isBlackout then
    print("Server is in blackout mode")
    
    -- Disable lighting systems
    TriggerEvent('lighting:blackout', true)
    
    -- Update UI for blackout
    SendNUIMessage({
        type = 'blackoutMode',
        active = true
    })
else
    -- Normal lighting
    TriggerEvent('lighting:blackout', false)
    
    SendNUIMessage({
        type = 'blackoutMode', 
        active = false
    })
end

Streamer Mode Integration

-- Monitor streamer mode changes
AddStateBagChangeHandler('streamermode', 'global', function(_, _, value)
    local isStreamerMode = value or false
    
    if isStreamerMode then
        print("Streamer mode enabled server-wide")
        
        -- Hide sensitive information
        TriggerEvent('streamer:hidePlates')
        TriggerEvent('streamer:hideNames')
        TriggerEvent('streamer:hideChat')
        
        -- Update UI
        SendNUIMessage({
            type = 'streamerMode',
            enabled = true
        })
    else
        print("Streamer mode disabled")
        
        -- Show normal information
        TriggerEvent('streamer:showPlates')
        TriggerEvent('streamer:showNames')
        TriggerEvent('streamer:showChat')
        
        SendNUIMessage({
            type = 'streamerMode',
            enabled = false
        })
    end
end)

-- Check current streamer mode status
local function isStreamerModeActive()
    return GlobalState.streamermode or false
end

-- Example usage in other systems
if isStreamerModeActive() then
    -- Hide license plates in vehicle display
    hideLicensePlates()
    
    -- Anonymize player names
    anonymizePlayerNames()
end

Localization System

-- Access localization data
local locales = GlobalState['ox_lib:locales']
if locales then
    print("Available locales:", json.encode(locales))
    
    -- Set up localization system
    TriggerEvent('locale:initialize', locales)
end

-- Monitor locale changes
AddStateBagChangeHandler('ox_lib:locales', 'global', function(_, _, value)
    if value then
        print("Localization data updated")
        TriggerEvent('locale:reload', value)
    end
end)

Group Configuration Access

-- Access group configuration
local policeGroup = GlobalState['group.police']
if policeGroup then
    print("Police group config:", json.encode(policeGroup))
    
    -- Use group configuration
    local permissions = policeGroup.permissions or {}
    local ranks = policeGroup.ranks or {}
    
    TriggerEvent('police:setConfig', {
        permissions = permissions,
        ranks = ranks
    })
end

-- Monitor group configuration changes
AddStateBagChangeHandler('group.police', 'global', function(_, _, value)
    if value then
        print("Police group configuration updated")
        TriggerEvent('police:updateConfig', value)
    end
end)

Object States

Object/prop entity states for non-vehicle, non-player entities like doors, props, and equipment.

Door & Access Control

StateTypeDescription
doorIdstringUnique identifier for door entities

Police Equipment

StateTypeDescription
inScopebooleanSpike strip is within interaction scope

Interactive Props

StateTypeDescription
invisiblebooleanMake object invisible

Development & Admin Tools

StateTypeDescription
entityIdstringDevelopment tool entity tracking ID
chopStagestableChop shop progression data
disableRepairbooleanPrevent repair
gearnumberGear state for mechanical objects

Usage Examples

Door System Integration

-- Set up door entity with ID
local function createDoor(doorData)
    local doorEntity = CreateObject(doorData.model, doorData.coords.x, doorData.coords.y, doorData.coords.z, false, false, false)
    
    -- Set door ID state
    Entity(doorEntity).state:set('doorId', doorData.id, true)
    
    -- Store door reference
    doorSystem[doorData.id] = {
        entity = doorEntity,
        data = doorData
    }
    
    print("Created door with ID:", doorData.id)
    return doorEntity
end

-- Monitor door interactions
local function setupDoorMonitoring()
    CreateThread(function()
        while true do
            local playerCoords = GetEntityCoords(PlayerPedId())
            
            for doorId, door in pairs(doorSystem) do
                local doorEntity = door.entity
                local doorState = Entity(doorEntity)?.state
                
                if doorState and doorState.doorId then
                    local doorCoords = GetEntityCoords(doorEntity)
                    local distance = #(playerCoords - doorCoords)
                    
                    if distance < 2.0 then
                        -- Player is near door
                        DrawText3D(doorCoords.x, doorCoords.y, doorCoords.z + 1.0, "Press E to interact with door")
                        
                        if IsControlJustPressed(0, 38) then -- E key
                            TriggerServerEvent('door:interact', doorState.doorId)
                        end
                    end
                end
            end
            
            Wait(0)
        end
    end)
end

Police Equipment (Spike Strips)

-- Spike strip scoping system
local spikeStrips = {}

-- Create spike strip
local function createSpikeStrip(coords, heading)
    local spikeModel = GetHashKey('p_ld_stinger_s')
    local spike = CreateObject(spikeModel, coords.x, coords.y, coords.z, true, false, false)
    
    SetEntityHeading(spike, heading)
    PlaceObjectOnGroundProperly(spike)
    FreezeEntityPosition(spike, true)
    
    -- Set initial scope state
    Entity(spike).state:set('inScope', false, true)
    
    spikeStrips[spike] = true
    return spike
end

-- Monitor spike strip proximity
CreateThread(function()
    while true do
        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        
        for spikeEntity, _ in pairs(spikeStrips) do
            if DoesEntityExist(spikeEntity) then
                local spikeCoords = GetEntityCoords(spikeEntity)
                local distance = #(playerCoords - spikeCoords)
                local spikeState = Entity(spikeEntity)?.state
                
                if spikeState then
                    local inScope = distance < 10.0
                    
                    -- Update scope state if changed
                    if spikeState.inScope ~= inScope then
                        Entity(spikeEntity).state:set('inScope', inScope, true)
                    end
                    
                    -- Show interaction if in scope
                    if inScope and distance < 2.0 then
                        DrawText3D(spikeCoords.x, spikeCoords.y, spikeCoords.z + 0.5, "Press E to pick up spike strip")
                        
                        if IsControlJustPressed(0, 38) then -- E key
                            TriggerServerEvent('police:removeSpikeStrip', spikeEntity)
                        end
                    end
                end
            else
                -- Clean up deleted entity
                spikeStrips[spikeEntity] = nil
            end
        end
        
        Wait(100)
    end
end)

Interactive Props System

-- Prop visibility system
local managedProps = {}

-- Create managed prop
local function createManagedProp(model, coords, invisible)
    local prop = CreateObject(model, coords.x, coords.y, coords.z, false, false, false)
    
    -- Set initial visibility state
    Entity(prop).state:set('invisible', invisible or false, true)
    
    -- Apply visibility
    if invisible then
        SetEntityVisible(prop, false, false)
        SetEntityCollision(prop, false, false)
    end
    
    managedProps[prop] = true
    return prop
end

-- Monitor prop visibility changes
AddStateBagChangeHandler('invisible', '', function(bagName, key, value)
    if bagName:find('entity:') then
        local entity = GetEntityFromStateBagName(bagName)
        
        if entity and managedProps[entity] then
            if value then
                -- Make invisible
                SetEntityVisible(entity, false, false)
                SetEntityCollision(entity, false, false)
                print("Made prop invisible:", entity)
            else
                -- Make visible
                SetEntityVisible(entity, true, false)
                SetEntityCollision(entity, true, true)
                print("Made prop visible:", entity)
            end
        end
    end
end)

-- Toggle prop visibility
local function togglePropVisibility(propEntity)
    local state = Entity(propEntity)?.state
    if state then
        local currentState = state.invisible or false
        Entity(propEntity).state:set('invisible', not currentState, true)
    end
end

Development Tools Integration

-- Entity tracking system
local trackedEntities = {}

-- Track entity for development
local function trackEntity(entity, customId)
    if not entity or entity == 0 then return end
    
    local entityId = customId or ('entity_' .. entity)
    Entity(entity).state:set('entityId', entityId, false)
    
    trackedEntities[entity] = {
        id = entityId,
        created = GetGameTimer(),
        coords = GetEntityCoords(entity)
    }
    
    print("Tracking entity:", entity, "with ID:", entityId)
end

-- Monitor tracked entities
CreateThread(function()
    while true do
        for entity, data in pairs(trackedEntities) do
            if DoesEntityExist(entity) then
                local state = Entity(entity)?.state
                if state and state.entityId then
                    -- Entity is still valid and tracked
                    local coords = GetEntityCoords(entity)
                    local distance = #(coords - data.coords)
                    
                    if distance > 1.0 then
                        -- Entity moved significantly
                        data.coords = coords
                        print("Tracked entity", state.entityId, "moved to:", coords)
                    end
                end
            else
                -- Entity no longer exists
                print("Tracked entity", data.id, "was deleted")
                trackedEntities[entity] = nil
            end
        end
        
        Wait(5000) -- Check every 5 seconds
    end
end)

Chop Shop Integration

-- Monitor chop shop progression
AddStateBagChangeHandler('chopStages', '', function(bagName, key, value)
    if bagName:find('entity:') then
        local entity = GetEntityFromStateBagName(bagName)
        
        if entity and IsEntityAVehicle(entity) then
            local stages = value or {}
            print("Chop stages updated for vehicle:", entity)
            
            -- Update visual effects based on stages
            for stage, completed in pairs(stages) do
                if completed then
                    TriggerEvent('chopshop:applyStageEffects', entity, stage)
                end
            end
        end
    end
end)

State Change Monitoring

Global State Monitoring Setup

-- Comprehensive global state monitoring
local function setupGlobalStateMonitoring()
    -- Time system monitoring
    AddStateBagChangeHandler('currentTime', 'global', function(_, _, value)
        if value then
            TriggerEvent('time:updated', value)
        end
    end)
    
    AddStateBagChangeHandler('freezeTime', 'global', function(_, _, value)
        TriggerEvent('time:freezeToggled', value)
    end)
    
    -- Weather monitoring
    AddStateBagChangeHandler('weather', 'global', function(_, _, value)
        if value then
            TriggerEvent('weather:changed', value)
        end
    end)
    
    AddStateBagChangeHandler('blackOut', 'global', function(_, _, value)
        TriggerEvent('power:blackoutToggled', value)
    end)
    
    -- Content monitoring
    AddStateBagChangeHandler('streamermode', 'global', function(_, _, value)
        TriggerEvent('content:streamerModeToggled', value)
    end)
end

-- Initialize monitoring
CreateThread(function()
    Wait(1000) -- Allow system to initialize
    setupGlobalStateMonitoring()
    print("Global state monitoring initialized")
end)

Object State Utilities

-- Safe object state access
local function getObjectState(object)
    if not object or object == 0 or not DoesEntityExist(object) then
        return nil
    end
    return Entity(object)?.state
end

-- Set object state safely
local function setObjectState(object, key, value, replicated)
    local state = getObjectState(object)
    if state then
        state:set(key, value, replicated or false)
        return true
    end
    return false
end

-- Batch object state operations
local function setMultipleObjectStates(object, states, replicated)
    local state = getObjectState(object)
    if not state then return false end
    
    for key, value in pairs(states) do
        state:set(key, value, replicated or false)
    end
    
    return true
end

Global states are synchronized to all clients automatically, while object states are only synchronized to clients that have the entity in scope.

Always check if objects exist before accessing their states, as entities can be deleted at any time. Use null-safe patterns consistently.

System states provide powerful integration points for server-wide functionality and object management across the entire game world.