Keeping engine on when exiting vehicle, my idea - am I overcomplicating it?

I’ve been thinking about how this could be done. I haven’t found anything by searching.

My idea is a boolean engineRunning (set by /engine <on/off> and a Thread.
(code hasn’t been tested and probably won’t run as it is)

lastDriverSeat = false # We don't know yet if player is driver
Citizen.CreateThread(function()
  while true to
    Wait(0)
    if IsPedInAnyVehicle(playerPed) then
      local vehicle = GetVehiclePedIsIn(playerPed)
    end
    if(GetPedInVehicleSeat(vehicle, -1) == playerPed then
      lastDriverSeat = true # Player is driver right now
    else
    if(lastDriverSeat == true) then # Player was driver last time we checked, but not anymore
      SetVehicleEngineOn(veh, engineRunning, false, true ) 
      lastDriverSeat = false
    end
  end
end)

Is there a simpler solution that I’m missing?