[HELP] Engine ON/OFF in my Menu

Hey,

I have a code here where you can switch the engine on and off, but I want it so that when you enter the car you can only turn on the engine in the menu, so it should not start automatically. Or when I leave the car it should stay on when the engine is on.

    function(data, menu)
      if data.current.value == 'engine' then
        local playerPed = GetPlayerPed(-1)
          local playerVeh = GetVehiclePedIsIn(playerPed, true)
        if engineOn == true then
                SetVehicleUndriveable(playerVeh, true)
            SetVehicleEngineOn(playerVeh, false, false, false)
            engineOn = false
            TriggerEvent("pNotify:SendNotification", {text = _U('turned_off_engine'), type = "error", queue = "vehiclemenu", timeout = 1000, layout = "bottomLeft"})
        else
          SetVehicleUndriveable(playerVeh, false)
            SetVehicleEngineOn(playerVeh, true, false, false)       
                engineOn = true
            TriggerEvent("pNotify:SendNotification", {text = _U('turned_on_engine'), type = "success", queue = "vehiclemenu", timeout = 1000, layout = "bottomLeft"})
        end
      end

Thanks for Answered <3

you made it?

Do any of you know where the automatic engine start is defined in QBCORE or Fivem? No matter what script I use, something always tries to start the engine when I get into the vehicle! If I suppress this with a code, then the hands always move to the ignition switch like crazy

A user named @Flatracer made this a mod a long time ago. It’s a bit buggy with helicopters (animation wise) when the engine was off, but it worked wonders:

Check this:

Use the code in the original post, or the trimmed down version and for the love of chin-chin, ask questions if you don’t understand…not just for you, but for others trying to do something similar!

You could try following:

local engineOn = false

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if IsControlJustReleased(1, 51) then -- 51 is the key 'E', change it ;D
            local playerPed = GetPlayerPed(-1)
            local playerVeh = GetVehiclePedIsIn(playerPed, false)
            if playerVeh and playerVeh ~= 0 then
                if engineOn then
                    SetVehicleEngineOn(playerVeh, false, false, false)
                    engineOn = false
                    TriggerEvent("pNotify:SendNotification", {text = _U('turned_off_engine'), type = "error", queue = "vehiclemenu", timeout = 1000, layout = "bottomLeft"})
                else
                    SetVehicleEngineOn(playerVeh, true, false, false)
                    engineOn = true
                    TriggerEvent("pNotify:SendNotification", {text = _U('turned_on_engine'), type = "success", queue = "vehiclemenu", timeout = 1000, layout = "bottomLeft"})
                end
            end
        end
    end
end)