[Release] Turn Signals/Indicators [Server-sided]

Thnkx

Another re-implementation without unnecessary variables for storing “GLOBAL” turn indicators states and without too many events

client.lua

local INDICATOR_INDEX = {
    left = 1,
    right = 0,
}

local INDICATORS_STATE = {
    off = 0,
    left = 1,
    right = 2,
    both = 3,
}

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        if IsControlJustPressed(1, 43) then -- Key [
            local ped = GetPlayerPed(-1)
            if IsPedInAnyVehicle(ped, true) then
                local vehicle = GetVehiclePedIsIn(ped, false)
                if GetPedInVehicleSeat(vehicle, -1) == ped then
                    local indicators = GetVehicleIndicatorLights(vehicle)

                    if indicators == INDICATORS_STATE.off then
                        indicators = INDICATORS_STATE.left
                    elseif indicators == INDICATORS_STATE.left then
                        indicators = INDICATORS_STATE.off
                    elseif indicators == INDICATORS_STATE.right then
                        indicators = INDICATORS_STATE.both
                    elseif indicators == INDICATORS_STATE.both then
                        indicators = INDICATORS_STATE.right
                    end

                    TriggerServerEvent('dp-turn-indicators:sync', indicators)
                end
            end
        end
 
        if IsControlJustPressed(1, 42) then -- Key ]
            local ped = GetPlayerPed(-1)
            if IsPedInAnyVehicle(ped, true) then
                local vehicle = GetVehiclePedIsIn(ped, false)
                if GetPedInVehicleSeat(vehicle, -1) == ped then
                    local indicators = GetVehicleIndicatorLights(vehicle)
                    
                    if indicators == INDICATORS_STATE.off then
                        indicators = INDICATORS_STATE.right
                    elseif indicators == INDICATORS_STATE.left then
                        indicators = INDICATORS_STATE.both
                    elseif indicators == INDICATORS_STATE.right then
                        indicators = INDICATORS_STATE.off
                    elseif indicators == INDICATORS_STATE.both then
                        indicators = INDICATORS_STATE.left
                    end

                    TriggerServerEvent('dp-turn-indicators:sync', indicators)
                end
            end
        end
    end
end)

RegisterNetEvent('dp-turn-indicators:sync')
AddEventHandler('dp-turn-indicators:sync', function(player_id, indicators)
    local player = GetPlayerFromServerId(player_id)
    local ped = GetPlayerPed(player)
    if not IsPedInAnyVehicle(ped, true) then return end

    local vehicle = GetVehiclePedIsIn(ped, false)
    if not DoesEntityExist(vehicle) then return end
    
    if indicators == INDICATORS_STATE.off then
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.left, false)
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.right, false)
    elseif indicators == INDICATORS_STATE.left then
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.left, true)
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.right, false)
    elseif indicators == INDICATORS_STATE.right then
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.left, false)
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.right, true)
    elseif indicators == INDICATORS_STATE.both then
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.left, true)
        SetVehicleIndicatorLights(vehicle, INDICATOR_INDEX.right, true)
    end
end)

server.lua

RegisterServerEvent('dp-turn-indicators:sync')
AddEventHandler('dp-turn-indicators:sync', function (indicators)
    local player_id = source
    TriggerClientEvent('dp-turn-indicators:sync', -1, player_id, indicators)
end)

Is there any way to do it with keymapping?

Thanks for the help

Yes, you take the code inside IsControlJustPressed() and put it inside commands (one command for each key), then you register the key mapping for those commands.