[HELP] Vehicle siren issue on server side

I currently have the below script to make it so when players that are in a police vehicle hit one of the following keybinds it does their proper action.
LCTRL + 1 - Sets their emergency vehicle to code one.
LCTRL + 2 - Sets their emergency vehicle to code two (lights no siren sound).
LCTRL + 3 - Sets their emergency vehicle to code three (lights and sirens).

It seems to work fine (for code one at least) but when going into code two’s it sets the vehicle to lights with no sirens for the client but in the server everyone else can still hear the sirens.

client.lua

-- code two --
RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function()
	local ped = PlayerPedId()
	local vehicle = GetVehiclePedIsIn(ped, false)
	local class = GetVehicleClass(vehicle)
	
	if vehicle ~= 0 then
		if class == 18 then 
			SetVehicleSiren(vehicle, true)
			DisableVehicleImpactExplosionActivation(vehicle, true)
			ShowNotification("You've entered ~y~code two~w~.")
		end
	end
end)
--------------

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        
        -- if player hits 'LCTRL + 2' - set police car to code 2
		if IsControlPressed(0, Keys['LEFTCTRL']) and IsControlJustPressed(0, Keys['2']) then 
			TriggerEvent('codeTwo')
		end
    end
end)

I would imagine I have to somehow transfer the event over to server side but not quite sure what exactly needs to go into the server event for it to be triggered for everyone. If someone could lead me to the right direction, it’d be greatly appreciated.

Thanks in advance,
Codi - lowheartrate

That’s it. TriggerClientEvent(eventName, -1, params)

How would I call that when a user hits the key LCTRL + 2 in the server.lua so when they hit the proper keybind it turns the siren sound off for everyone on the server?

TriggerEvent('codeTwo') you are triggering the event from the current user to the current user.

So should I throw

RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function()
	local ped = PlayerPedId()
	local vehicle = GetVehiclePedIsIn(ped, false)
	local class = GetVehicleClass(vehicle)
	
	if vehicle ~= 0 then
		if class == 18 then 
			SetVehicleSiren(vehicle, true)
			DisableVehicleImpactExplosionActivation(vehicle, true)
			ShowNotification("You've entered ~y~code two~w~.")
		end
	end
end)

inside of my server.lua and then use TriggerServerEvent('codeTwo') when the user hits the proper keybind (LCTRL + 2).

I’ve added

-- toggle code two --
RegisterServerEvent("codeTwo")
AddEventHandler("codeTwo", function(amount)
	local ped = PlayerPedId()
	local vehicle = GetVehiclePedIsIn(ped, false)
	local class = GetVehicleClass(vehicle)
	
	if vehicle ~= 0 then
		if class == 18 then 
			SetVehicleSiren(vehicle, true)
			DisableVehicleImpactExplosionActivation(vehicle, true)
			ShowNotification("You've entered ~y~code two~w~.")
		end
	end
end)
---------------------

to my server.lua and replaced the check for the proper bind to

-- if player hits 'LCTRL + 2' - set police car to code 2
if IsControlPressed(0, Keys['LEFTCTRL']) and IsControlJustPressed(0, Keys['2']) then 
	TriggerServerEvent('codeTwo')
end

but it doesn’t seem to work. When hitting the bind, nothing happens and I get no errors.

Ive had this problem before. this is because LUA has a funky issue:

Strings can be denoted with either " OR ’ … BUT lua often doesnt recognize the two different versions as the same thing. probably they are two different types of strings in terms of bytes, but I dont know that much about lua.

anyway. Because you have your event in server.lua with the name “codeTwo” in " " … but the trigger event in Client is ‘codeTwo’ in ’ ’ … they are not the same call.

so just go in and make sure you use all the same symbol in all places (either “” or ’ ’ … not both)

should solve your issue

Seems to be calling the function but getting an error in my server console:

Error running system event handling function for resource lhr_vehicleController: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: @lhr_vehicleController/server.lua:89: attempt to call a nil value (global 'PlayerPedId')
stack traceback:
        @lhr_vehicleController/server.lua:89: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:219: in function <citizen:/scripting/lua/scheduler.lua:218>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:218: in function <citizen:/scripting/lua/scheduler.lua:182>

server.lua:89 : local ped = PlayerPedId()

try PlayerPedId(source)

remember this is a server script so the server itself has no ped

also, in order to update all the clients online from the server script, you have to get the list of players and push each one a ClientEvent separately, like this:

in server.lua =

RegisterServerEvent('codeTwo')
AddEventHandler('codeTwo', function()
       ...  do stuff and then...       
                local players = GetPlayers()
                for _,player in pairs(players) do
                    TriggerClientEvent('codeTwo', player)
                    
                    --print("sent to player: " .. player)
                end
end)

I tried PlayerPedId(source) but it still threw this error in the console:
attempt to call a nil value (global 'PlayerPedId')

hmm the event should pass its source automatically but try adding this in your event in server lua:

local source = source

Unsure why but added that and still getting this error :frowning:

-- toggle code two --
RegisterServerEvent('codeTwo')
AddEventHandler('codeTwo', function(amount)
	local source = source
	local ped = PlayerPedId(source)
	local vehicle = GetVehiclePedIsIn(ped, false)
	local class = GetVehicleClass(vehicle)
	
	if vehicle ~= 0 then
		if class == 18 then 
			SetVehicleSiren(vehicle, true)
			DisableVehicleImpactExplosionActivation(vehicle, true)
			ShowNotification("You've entered ~y~code two~w~.")
		end
	end
end)
Error running system event handling function for resource lhr_vehicleController: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: @lhr_vehicleController/server.lua:90: attempt to call a nil value (global 'PlayerPedId')
stack traceback:
        @lhr_vehicleController/server.lua:90: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:219: in function <citizen:/scripting/lua/scheduler.lua:218>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:218: in function <citizen:/scripting/lua/scheduler.lua:182>

hmm well you can always force the source through.

in client:

TriggerServerEvent(‘codeTwo’, source)

in server:

AddEventHandler(‘codeTwo’, function(source, amount)

also now that I look at it, I dont think you are filling or using amount so you can get rid of it in server

AddEventHandler(‘codeTwo’, function(source)

I’ve adjusted those two but unfortunately getting nothing but the same error :frowning:

maybe try GetPlayerPed(source) instead ?

Doing that it gave me a different error:

InvokeNative: execution failed: Argument at index 0 was null.
Error running system event handling function for resource lhr_vehicleController: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: Execution of native 000000006e31e993 in script host failed.
stack traceback:
        [C]: in upvalue '_in'
        citizen:/scripting/lua/natives_server.lua:202: in function 'GetPlayerPed'
        @lhr_vehicleController/server.lua:90: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:219: in function <citizen:/scripting/lua/scheduler.lua:218>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:218: in function <citizen:/scripting/lua/scheduler.lua:182>

uh

maybe try…

local _source = source
local ped = PlayerPedId(_source)

and that takes me back to the previous error I was getting. attempt to call a nil value (global 'PlayerPedId')

maybe

local _source = source
local playerIdx = GetPlayerFromServerId(_source)
local ped = GetPlayerPed(playerIdx)