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 'GetPlayerFromServerId')
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 im at a loss.

local ped = PlayerPedId(source)

should work according to the fivem reference:

https://runtime.fivem.net/doc/natives/#_0xD80958FC74E988A6

it is definitely available to a server function

something weird about how source is reacting is happening

in the __resource.lua have you specified a manifest version?

Yes - resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

i have no idea then :confused:

1 Like

PlayerPedId() works on the client side right?

try this

in client:

local ped = PlayerPedId()
TriggerServerEvent(‘codeTwo’, ped)

in server:


RegisterServerEvent('codeTwo')
AddEventHandler('codeTwo', function(clientPed)
             local ped = clientPed

jesus I just realized what you are trying to send is the state of the vehicles lights/siren right? hehe we actually need to be sending the VEHICLE through, not the player ped

will also need to send the vehicle back tot he client when you update clients at the end of the server event

1 Like

Yes I am trying to set it so it updates for all the players (no siren sound) because as of right now with the code I originally posted, it turns siren sound off for only the client that called the event.

so…

client:


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 
			local ped = PlayerPedId()
			local vehicle = GetVehiclePedIsIn(ped, false)
			local class = GetVehicleClass(vehicle)
			
			if vehicle ~= 0 then
				if class == 18 then 
					
					TriggerServerEvent('codeTwo', vehicle)
					ShowNotification("You've entered ~y~code two~w~.")
				end
			end

			
		end
    end
end)

-- code two --
RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function(vehicle)
	SetVehicleSiren(vehicle, true)
	DisableVehicleImpactExplosionActivation(vehicle, true)
	
end)
--------------

server:


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

(edited to move notification to just the client who presses the key)

actually the notification should probably still be in the client thread after the TriggerServerEvent

1 Like

Worked great, thank you. Do I need to adjust the location of the ShowNotification? Seems to notify me just fine and set code two’s for the players vehicle great!

if you leave it in the event that is called for all players, then all player will get the notification

Right, so its in the

-- code two --
RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function(vehicle)
	SetVehicleSiren(vehicle, true)
	DisableVehicleImpactExplosionActivation(vehicle, true)
					
	ShowNotification("You've entered ~y~code two~w~.")
end)
--------------

Where is it that it should go?

right after

is probably best

1 Like

Gotcha, thank you!

glad i could help :smiley:

1 Like

Got it tested and working on my server for codeTwo but tried to do the same server event with codeThree but doesn’t seem to do anything at all including turning on the lights at all. I tried to set it as just a client event but run into the issue where other players cannot hear the siren again so I used the codeTwo we did as a template and did the same method but it doesn’t set the siren sound for even me. I get 0 errors in the F8 Console and still get the ShowNotification("You've entered ~y~code three~w~.").

client.lua

-- code three --
RegisterNetEvent('codeThree')
AddEventHandler('codeThree', function()
	-- for server event -- 
    SetVehicleSiren(vehicle, true)
    DisableVehicleImpactExplosionActivation(vehicle, false)
end)

Citizen.CreateThread(function()
    while true do
      Citizen.Wait(0)
      
      -- if player hits 'LCTRL + 3' - set police car to code 3
      if IsControlPressed(0, Keys['LEFTCTRL']) and IsControlJustPressed(0, Keys['3']) then 
	    local player = PlayerPedId()
	    local vehicle = GetVehiclePedIsIn(player, false)
	    local class = GetVehicleClass(vehicle)
	
	    if vehicle ~= 0 then 
		  if class == 18 then 
		    TriggerServerEvent('codeThree', vehicle)
		    ShowNotification("You've entered ~y~code three~w~.")
		  end 
	    end 
      end

    end
end)

server.lua

-- toggle code three --
RegisterServerEvent('codeThree')
AddEventHandler('codeThree', function(vehicle)
	local players = GetPlayers()
	for _,player in pairs(players) do
		TriggerClientEvent('codeThree', player, vehicle)
		
		--print("sent to player: " .. player)
	end
end)
-----------------------

looking at that code it looks like it should all work. must be something really minor

the best thing to do is insert some print() functions to see where the code stops working.
start by uncommenting the print function in the server
and add a print function in the client event as well so you can see if it gets back to the client

It actually seems to be real broken at the moment. I had it in my server last night and when I toggled code two/three, it changed the siren control for another player and didn’t touch mine although it gave me the notification. I have uploaded the project to my GitHub so you can easily see what my code looks like if you don’t mind taking a look? https://github.com/lowheartrate/vehicleController. In the meantime, i’ll go about using print() to see where it stops working properly.

in the code you put on github, for the codeTwo and codeThree client events you have them finding new targets without having a source.

-- code two --
RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function(vehicle)
	local player = GetPlayerPed()
	local vehicle = GetVehiclePedIsIn(player, false)
	local class = GetVehicleClass(vehicle)
	-- turn on siren 
	SetVehicleSiren(vehicle, true)
	-- turn off siren (sound)
	DisableVehicleImpactExplosionActivation(vehicle, true)
end)
--------------

-- code three --
RegisterNetEvent('codeThree')
AddEventHandler('codeThree', function()
	local player = GetPlayerPed()
	local vehicle = GetVehiclePedIsIn(player, false)
	local class = GetVehicleClass(vehicle)
	-- for server event -- 
	SetVehicleSiren(vehicle, true)
	DisableVehicleImpactExplosionActivation(vehicle, false)

the vehicle is already determined and being sent along tot he server and back tot he client, so you dont want to try to find a new one.

the code should look like:

-- code two --
RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function(vehicle)
	-- turn on siren 
	SetVehicleSiren(vehicle, true)
	
	DisableVehicleImpactExplosionActivation(vehicle, true)
end)
--------------

-- code three --
RegisterNetEvent('codeThree')
AddEventHandler('codeThree', function()
	-- turn on siren
	SetVehicleSiren(vehicle, true)
	DisableVehicleImpactExplosionActivation(vehicle, false)
end)

of course I realize now this code will only make the lights turn on, not off.

to make it toggle:

-- code two --
RegisterNetEvent('codeTwo')
AddEventHandler('codeTwo', function(vehicle)
	if IsVehicleSirenOn(vehicle) then
              SetVehicleSiren(vehicle, false)
        else
	          SetVehicleSiren(vehicle, true)
        end
	
	DisableVehicleImpactExplosionActivation(vehicle, true)
end)
--------------

-- code three --
RegisterNetEvent('codeThree')
AddEventHandler('codeThree', function()
	if IsVehicleSirenOn(vehicle) then
              SetVehicleSiren(vehicle, false)
        else
	          SetVehicleSiren(vehicle, true)
        end
	DisableVehicleImpactExplosionActivation(vehicle, false)
end)

also in the Citizen thread you might want to check for toggle as well:


		-- if player hits 'LCTRL + 2' - set police car to code 2
		if IsControlPressed(0, Keys['LEFTCTRL']) and IsControlJustPressed(0, Keys['2']) then 
			-- set variables 
			local player = PlayerPedId()
			local vehicle = GetVehiclePedIsIn(player, false)
			local class = GetVehicleClass(vehicle)
			
			-- if player is in a vehicle 
			if vehicle ~= 0 then 
				-- if vehicle ped is in, is an emergency vehicle 
				if class == 18 then 
					-- trigger codeTwo server event
					TriggerServerEvent('codeTwo', vehicle)
					-- give user notification
                                       if IsVehicleSirenOn(vehicle) then
                                           ShowNotification("You've canceled ~y~code two~w~.")
                                       else
	                                       ShowNotification("You've entered ~y~code two~w~.")
                                       end
					
				end
			end
		end
		
		-- if player hits 'LCTRL + 3' - set police car to code 3
		if IsControlPressed(0, Keys['LEFTCTRL']) and IsControlJustPressed(0, Keys['3']) then 
			local player = PlayerPedId()
			local vehicle = GetVehiclePedIsIn(player, false)
			local class = GetVehicleClass(vehicle)
			
			if vehicle ~= 0 then 
				if class == 18 then 
					TriggerServerEvent('codeThree', vehicle)
                                        if IsVehicleSirenOn(vehicle) then
                                           ShowNotification("You've canceled ~y~code three~w~.")
                                       else
	                                       ShowNotification("You've entered ~y~code three~w~.")
                                       end
				end 
			end 
		end