Jump to content

Vehicle lock


Xwad

Recommended Posts

  • 3 weeks later...

hi i test it and it dosent work:/

function lockcar ( thePlayer )

playervehicle = getPlayerOccupiedVehicle ( thePlayer ) -- define 'playervehicle' as the vehicle the player is in

if ( playervehicle ) then -- if a player is in a vehicle

if isVehicleLocked ( playervehicle ) then -- and if the vehicle is already locked

setVehicleLocked ( playervehicle, false ) -- unlock it

else -- otherwise (if it isn't locked)

setVehicleLocked ( playervehicle, true ) -- lock it

end

end

end

function bindLockOnSpawn ( theSpawnpoint ) -- when a player spawns

bindKey ( source, "l", "down", "Lock car", lockcar ) -- bind the 'l' key to the 'lockcar' function

end

addEventHandler ( "onPlayerSpawn", getRootElement(), bindLockOnSpawn ) -- add an event handler for onPlayerSpawn

Link to comment
addEventHandler ( "onVehicleStartEnter", root, 
    function ( player, seat ) 
        if ( isElement ( getVehicleOccupant ( source ) ) and seat == 0 ) then 
            cancelEvent ( ) 
        end 
    end 
) 

Ah yea, don't know how this 'source' instead of 'root' got there. Thank XeoN-.

His code is correct. It was supposed to be 'source', not 'root'.

Link to comment

404d7793b025e1077db4657db7d9aa32.png

source is the element that triggered the event (the vehicle that was being entered)

By default, all scripts have a predefined root.

It is almost as if every script had

root = getRootElement() 

on the top.

The root element is the element that is the parent of everything. Read more on Wiki: Element_tree

Executing a function on root will execute it on all (possible) elements. Eg, setVehicleLocked(root, true) will lock ALL vehicles.

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