Jump to content

Off automatic start engine


mehmet

Recommended Posts

Hello guys!
I need help. I am newbie.I trying to do save the vehicle engine state, manual start engine and stop engine. 

This is my code. Bad code, but I'm learning
 


function enterVehicle () 
    if getVehicleEngineState(theVehicle) == false then
        setVehicleEngineState(theVehicle, false)
    elseif getVehicleEngineState(theVehicle) == true then
        setVehicleEngineState(theVehicle, true)
    end 
end
addEventHandler ( "onPlayerVehicleEnter", getRootElement(), enterVehicle )

 


Edited by mehmet
Link to comment
function enterVehicle () 
    if getVehicleEngineState(source) == true then
        setVehicleEngineState(source, false)
    else
        setVehicleEngineState(source, true)
    end 
end
addEventHandler ( "onVehicleEnter", root, enterVehicle)

You were using the wrong event. The right event is onVehicleEnter

Also, the source element of this event is the vehicle that was entered.

Link to comment

Try this :)

function enterVehicle () 
    if getVehicleEngineState(source) == false then
        setVehicleEngineState(source, true)
    end
end
addEventHandler ( "onVehicleEnter", root, enterVehicle)

function exitVehicle () 
    if getVehicleEngineState(source) == true then
        setVehicleEngineState(source, false)
    end
end
addEventHandler ( "onVehicleExit", root, enterVehicle)

 

Link to comment
EngineTable = {}

function enterVehicle () 
        setVehicleEngineState(source, EngineTable[source] or false)
  EngineTable[source] = nil
end
addEventHandler ( "onVehicleEnter", root, enterVehicle)

function exitVehicle () 
EngineTable[source] = getVehicleEngineState(source) 
end
addEventHandler ( "onVehicleExit", root, enterVehicle)

 

Link to comment

CodyJ, when I sit in the car, engine is automatically started. 
I want to manually
here is my code


 

function turnEngine(player) 
    local car = getPedOccupiedVehicle(player) 
    if(car and getVehicleController(car) == player) then 
        setVehicleEngineState(car,(not getVehicleEngineState(car))) 
        if(getVehicleEngineState(car)) then 
            outputChatBox("Двигатель запущен", player, 0, 255, 0) 
        else 
            outputChatBox("Двигатель заглушен", player, 255, 0, 0) 
        end 
    end 
end 
addCommandHandler ("engine",turnEngine) 


EngineTable = {}

function enterVehicle () 
        setVehicleEngineState(source, EngineTable[source] or false)
  EngineTable[source] = nil
end
addEventHandler ( "onVehicleEnter", root, enterVehicle)

function exitVehicle () 
EngineTable[source] = getVehicleEngineState(source) 
end
addEventHandler ( "onVehicleExit", root, enterVehicle)

 

Edited by mehmet
Link to comment
2 hours ago, xeon17 said:

Try this :)


function enterVehicle () 
    if getVehicleEngineState(source) == false then
        setVehicleEngineState(source, true)
    end
end
addEventHandler ( "onVehicleEnter", root, enterVehicle)

function exitVehicle () 
    if getVehicleEngineState(source) == true then
        setVehicleEngineState(source, false)
    end
end
addEventHandler ( "onVehicleExit", root, enterVehicle)

 

Try the same code but with exitVehicle instead of enterVehicle on the event handler on 13th line.

  • Like 1
Link to comment

I don't understand what are you trying to do. The following code will disable the engine when you leave the vehicle.

And you can enable or disable the engine by typing /engine (if you are the driver)

addEventHandler("onVehicleExit", root,
function (player, seat)
	if (seat == 0) then
        if (getVehicleEngineState(source) == true) then
            setVehicleEngineState(source, false)
        end
    end
end)

 addCommandHandler("engine", 
 function (player, command)
 	local vehicle = getPedOccupiedVehicle(player) 
        if(vehicle and getVehicleController(vehicle) == player) then 
        	if (getVehicleEngineState(vehicle) == true) then
        		setVehicleEngineState(vehicle, false)
        	else
        		setVehicleEngineState(vehicle, true)
            end
        end
    end
end)

 

Link to comment

Remove the onVehicleExit event handler then. You can disable all vehicle engines with a loop, 

 addEventHandler("onResourceStart", resourceRoot,
 function ()
 	for _,vehicle in pairs (getElementsByType("vehicle")) do
 		if (getVehicleEngineState(vehicle) == true) then
 			setVehicleEngineState(vehicle, false)
 		end
 	end
 end)

And then just enter in the vehicle and type /engine.

Link to comment

xeon, here is my code.Does not help. I enter in the car and he automatically starts the engine

Quote

 addEventHandler("onResourceStart", resourceRoot,
 function ()
    for _,vehicle in pairs (getElementsByType("vehicle")) do
        if (getVehicleEngineState(vehicle) == true) then
            setVehicleEngineState(vehicle, false)
        end
    end
 end)

 addCommandHandler("engine", 
 function (player, command)
    local vehicle = getPedOccupiedVehicle(player) 
        if(vehicle and getVehicleController(vehicle) == player) then 
            if (getVehicleEngineState(vehicle) == true) then
                setVehicleEngineState(vehicle, false)
            else
                setVehicleEngineState(vehicle, true)
            end
        end
end)

 


Edited by mehmet
Link to comment
addEventHandler("onVehicleEnter", root, -- when entering any vehicle
  function (player, seat)
    if (seat == 0) then -- as the driver,
      if (getVehicleEngineState(source)==false) then -- check if the engine is off
        setVehicleEngineState(source, false) -- and keep it off
      end
    end
  end
)

This should work so long as the getVehicleEngineState returns the state right before the game starts the engine automatically... If this is not the case, your only recourse is to try a table or element data to store the engine states of every car, and force that engine state when a player enters it (changing the data or table value when engine is manually toggled).

Link to comment

 

EngineTable = {}

function enterVehicle () 
        setVehicleEngineState(source, EngineTable[source] or false)
  EngineTable[source] = nil
end
addEventHandler ( "onVehicleEnter", root, enterVehicle)

function exitVehicle () 
EngineTable[source] = getVehicleEngineState(source) 
end
addEventHandler ( "onVehicleExit", root, exitVehicle)

function setEngine()
setVehicleEngineState(getPedOccupiedVehicle(player),not getPedOccupiedVehicle(player))
end

addCommandHandler("engine",setEngine)

Try

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...