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.
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?
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).
-- 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)
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>
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)
Unsure why but added that and still getting this error
-- 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>
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>