Leaving engine on after player exits vehicle

Hi All,

I have an issue with a bit of code I have wrote:

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

		local ped = GetPlayerPed(-1)
		
		if DoesEntityExist(ped) and IsPedInAnyVehicle(ped, false) and IsControlPressed(2, 75) and not IsEntityDead(ped) and not IsPauseMenuActive() then
			local engineWasRunning = GetIsVehicleEngineRunning(GetVehiclePedIsIn(ped, true))
			Citizen.Wait(1000)
			if DoesEntityExist(ped) and not IsPedInAnyVehicle(ped, false) and not IsEntityDead(ped) and not IsPauseMenuActive() then
				local veh = GetVehiclePedIsIn(ped, true)
				if (engineWasRunning) then
					SetVehicleEngineOn(veh, true, true, true)
				end
			end
		end
	end
end)

The engines of sedans & compacts etc get turned back on after the player leaves the vehicle however when a player exits one of the super cars like T20/Vacca/Thrax/Reaper/Itali GTB etc etc the engine is turned off when the player leaves the vehicle and is not being turned back on.

Would you have any advice what I could do to fix this?

Thanks a lot in advance!

Hello!

Did you write this, or did you take it from here? It looks a bit similar.

Either way, try this.

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

		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped, false) then
			local veh = GetVehiclePedIsIn(ped, false)

			if GetIsVehicleEngineRunning(veh, false) then
				if IsControlPressed(2, 75) then
					Citizen.Wait(250)
					SetVehicleEngineOn(veh, true, true, true)
					TaskLeaveVehicle(ped, veh, false, 0)
				end
			else
				Citizen.Wait(1000)
			end
		else
			Citizen.Wait(1500)
		end

	end
end)

This should work, I’m not 100% certain. If it doesn’t work, then I’d recommend looking for scripts that do something similar (google: fivem leave vehicle engine running). There are plenty out there.