mehmet Posted February 12, 2017 Share Posted February 12, 2017 (edited) 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 February 12, 2017 by mehmet Link to comment
xeon17 Posted February 12, 2017 Share Posted February 12, 2017 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
mehmet Posted February 12, 2017 Author Share Posted February 12, 2017 (edited) xeon17, ty man! But when engine ON and i get out of the car, after I get in the car, engine is off. How to fix it? Edited February 12, 2017 by mehmet Link to comment
xeon17 Posted February 12, 2017 Share Posted February 12, 2017 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
mehmet Posted February 12, 2017 Author Share Posted February 12, 2017 xeon17, when I go out of the car, engine is started Link to comment
Captain Cody Posted February 12, 2017 Share Posted February 12, 2017 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
mehmet Posted February 12, 2017 Author Share Posted February 12, 2017 (edited) 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 February 12, 2017 by mehmet Link to comment
Addlibs Posted February 12, 2017 Share Posted February 12, 2017 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. 1 Link to comment
xeon17 Posted February 12, 2017 Share Posted February 12, 2017 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
mehmet Posted February 12, 2017 Author Share Posted February 12, 2017 xeon, I'm trying to make a motor system as a role play. When I get in the car. Engine is automatically started. How to make sure to not start automatically? Link to comment
NeXuS™ Posted February 12, 2017 Share Posted February 12, 2017 addEventHandler("onPlayerVehicleEnter", getRootElement(), function(theVeh) if not getVehicleEngineState(theVeh) then setVehicleEngineState(theVeh, false) end end) And then just add a commandHandler to a startEngine function. Link to comment
xeon17 Posted February 12, 2017 Share Posted February 12, 2017 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
mehmet Posted February 12, 2017 Author Share Posted February 12, 2017 (edited) 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 February 12, 2017 by mehmet Link to comment
Addlibs Posted February 12, 2017 Share Posted February 12, 2017 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
Captain Cody Posted February 12, 2017 Share Posted February 12, 2017 My code saves what you had your engine set at when you left it and loads it when you reenter that is basically exactly what you're trying to do. Link to comment
xeon17 Posted February 12, 2017 Share Posted February 12, 2017 As far as i know, the game does not automatically turn the engine on. If i'm wrong, combine my code with the code of MrTasty. Link to comment
Captain Cody Posted February 13, 2017 Share Posted February 13, 2017 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
mehmet Posted February 13, 2017 Author Share Posted February 13, 2017 Working! Thank you all who helped!!! CodyJ, xeon, MrTasty thanks you! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now