Jump to content

Stop engine when key pressed !


zapezel

Recommended Posts

I'm begginer :"> .I made a script to stop the engine when i press a key but didn't work :(

local root = getRootElement ()
local thisResourceRoot = getResourceRootElement(getThisResource())
 
function thisResourceStart ()
local players = getElementsByType ( "player" )
bindKey ( source, "1", "down", stopEngine, "Lights on/off" )
end
 
addEventHandler ( "onResourceStart", thisResourceRoot, thisResourceStart )
 
function stopEngine ( player, key, state )
if ( getPedOccupiedVehicleSeat ( player ) == 0 ) then
local vehicle = getPedOccupiedVehicle ( player )
if ( getVehicleEngineState ( vehicle ) == true ) then
setVehicleEngineState ( vehicle, false)
else
setVehicleEngineState ( vehicle, true )
end
end
end

Link to comment

When resource starts, to bind the key for all players, you must loop through them.

function thisResourceStart ()
local players = getElementsByType ( "player" )
for _,this_player in ipairs(players) do
bindKey ( this_player, "1", "down", stopEngine, "Lights on/off" )
end
end

To bind the key for players who join when resource has already started, put bindKey into function triggered by onPlayerJoin event.

function playerJoined()
bindKey ( source, "1", "down", stopEngine, "Lights on/off" )
end
addEventHandler("onPlayerJoin",root,playerJoined)

Function stopEngine seems to be fine, but we can shorten its code.

function stopEngine ( player, key, state )
if getPedOccupiedVehicleSeat ( player ) == 0 then
local vehicle = getPedOccupiedVehicle ( player )
setVehicleEngineState ( vehicle, not getVehicleEngineState ( vehicle ) )
end
end

By the way, I don't see what does that function argument "Lights on/off" mean :)

Link to comment

I don't know even all the words from english , I'm from Romania and my english lessons are very poor :-<.

I need to replace "Light" with "motor or engine" !

Thanks a lot man ! I apreciate your help !

Regards from Zapezel

Regards from Romania

Link to comment

function thisResourceStart ()
   local players = getElementsByType ( "player" )
   for _,this_player in ipairs(players) do
       bindKey ( this_player, "1", "down", stopEngine, "Lights on/off" )
   end
end

This will not work if we don't write a event :>.like this :D

without this when you will restart the resource or start when you have players that bindkey will not work.Else will work when a player join :>

addEventHandler ("onResourceStart", getRootElement(), thisResourceStart)

Link to comment
Salut , si eu sunt din romania

vreau doar sa iti spun ca daca iti trebuie ceva sa imi ai un PM :D

This forum is English only. If you say something in your language, translate it to English.

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