Jump to content

[HELP] setVehicleEngineState problem!


griff316

Recommended Posts

Posted

Hey guys!
I've a problem with setVehicleEngineState. When I use it to turn off the engine, the engine immediately turns on and I don't know why. I hope you can help me.

(Sorry for my bad english:S)

Posted

No matter how i try, its not working...

function engine (player, command)
  if isPedInVehicle(player) then
    local vehicle = getPedOccupiedVehicle(player)
    local state = getVehicleEngineState(vehicle)
    setVehicleEngineState(vehicle, not state)
  else return false end
end
addCommandHandler("engine", engine)

 

Posted (edited)

Hello, in LUA the return is like end or break when you use for.

 

 

So, remove the end after the return false.

Edited by Gordon_G
Posted

Try this ;

 

function engine ( aPlayer )
  if not isPedInVehicle ( aPlayer ) then return end
    setVehicleEngineState ( getPedOccupiedVehicle ( aPlayer ),not getVehicleEngineState ( getPedOccupiedVehicle ( aPlayer ) ) )
end
addCommandHandler ( 'engine',engine )

 

Posted (edited)
function toggleEngine(thePlayer)
    if isPedInVehicle(player) then
	    local theVehicle = getPedOccupiedVehicle(thePlayer)
        if getPedOccupiedVehicleSeat(thePlayer) == 0 then
		   if (getVehicleEngineState(theVehicle) == true) then
		       setVehicleEngineState(theVehicle, false)
	       else
		      setVehicleEngineState(theVehicle, true)
		   end
		else
		   outputChatBox("You but be the driver for use that command!", thePlayer, 255, 0, 0)
       end
	else
	    outputChatBox("You must be in a vehicle to use that command!", thePlayer, 255, 0, 0)
	end
end
addCommandHandler("engine", toggleEnginea)

 

Edited by Dimos7
Posted
20 minutes ago, Dimos7 said:

function toggleEngine(thePlayer)
    if isPedInVehicle(player) then
	    local theVehicle = getPedOccupiedVehicle(thePlayer)
        if getPedOccupiedVehicleSeat(thePlayer) == 0 then
		   if (getVehicleEngineState(theVehicle) == true) then
		       setVehicleEngineState(theVehicle, false)
	       else
		      setVehicleEngineState(theVehicle, true)
		   end
		else
		   outputChatBox("You but be the driver for use that command!", thePlayer, 255, 0, 0)
       end
	else
	    outputChatBox("You must be in a vehicle to use that command!", thePlayer, 255, 0, 0)
	end
end
addCommandHandler("engine", toggleEnginea)

 

 

Your code is to long also you have an extra 'a' in the addCommandHandler function in the function name part.

Posted (edited)

Includes the chat box messages, and semi compact format.

function engine ( aPlayer )
local vehicle = getPedOccupiedVehicle ( aPlayer )
  if not vehicle then outputChatBox("You must be in a vehicle",aPlayer) return end
		setVehicleEngineState ( getPedOccupiedVehicle ( aPlayer ),not getVehicleEngineState (vehicle) )
	if getVehicleEngineState (vehicle) then
		outputChatBox("Engine has been turned on",aPlayer)
	else
		outputChatBox("Engine has been turned off",aPlayer)
	end
end
addCommandHandler ( 'engine',engine )

Could compact it more using an and statement though.

Edited by CodyL
Posted (edited)

I tried all of your codes, but the problem is same.

Im thikning maybe a basic resource in mta, or similar. I dont know. Any idea?

Edited by griff316
Posted

When the engine turns off, immediately i hear the basic engine start sound and the engine turns on. It happens every second time. So it detect, but the engine didnt stop.

Posted (edited)

Just a thought, but if the codes posted above do not work for you... maybe there's another resource forcing the engine on? Are you trying the resource in a default server (no other resource other than defaults ones)? If not, try stopping all resources. Check if it works. If it works, start one resource at a time and check everytime if it works. If you have lots of resources, you can start them in blocks, and when you encounter the problem, probably the resource that is causing the problem is in that block of resources. If no resource causes problem (it does not work even if all non-default res are stopped) then i'm missing something :(

 

1 minute ago, iPrestege said:

You mean that when you enter the vehicle and the engine was off and it start?

If i understood correctly, when he turns off the engine it automatically turns on again. 

@griff316 but what @iPrestege said lead me to think one thing: what happens if you (via script) turn off the engine when you are not in the vehicle (if you are testing while in it) (or inside it if you are testing when you are not in the vehicle)? (do not do this if you do not want to, it's optional. Do what i wrote above first)

Edited by LoPollo
  • Like 1
Posted

:oops: :signthanks:

Thanks all of your help, its solved. I written a car script 1 years ago, wich is prevent car blow and I forgot that I written a line to in wich turns the engine on when vehicle health is more then the min value. One more time thanks, its my fault, sorry. :oops:

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...