[SOLVED] Trying to SetVehicleEngineOn

I am trying to create a script that sets the vehicle engine on/off according to the vehicles current engineStatus but when the player hits the keybind ( LCTRL + W ) it doesn’t turn the engine on. I know the keybind is working fine because it displays the proper You're not in a vehicle. or You are in a vehicle. message.

-- if player hits 'LCTRL + W' - toggle engine 
if IsControlPressed(0, Keys['LEFTCTRL']) and IsControlJustPressed(0, Keys['W']) then
	local ped = PlayerPedId()
	local vehicle = GetVehiclePedIsIn(ped, false)
	local engineStatus = GetIsVehicleEngineRunning(vehicle)
	
	if vehicle == 0 then 
		ShowNotification("~r~You're not in a vehicle.")
	else 
		ShowNotification("~b~You are in a vehicle.")

		if not engineStatus then
			SetVehicleEngineOn( vehicle, true, true )
		else 
			SetVehicleEngineOn( vehicle, false, true )
		end 
	end
end

SOLVED:

local ped = PlayerPedId()
local vehicle = GetVehiclePedIsIn(ped, false)
local engineStatus = GetIsVehicleEngineRunning(vehicle)

if vehicle ~= 0 then
	if not engineStatus then
		SetVehicleEngineOn(vehicle, true, false, true)
		ShowNotification("You've turned the key into the ~g~on ~w~position.")
	else 
		SetVehicleEngineOn(vehicle, false, false, true)
		ShowNotification("You've turned the key into the ~r~off ~w~position.")
	end 
end
1 Like