Jump to content

Help with Event Handlers


Play3rPL

Recommended Posts

Hello,

I have a problem with two event handlers at the end of my code.

One of these seems ignoring marker, that I created inside of my If statement declared in function.

That variable is NOT declared as local, so I have completly no idea, why

it is ignoring my marker.

And I don't know, what is wrong with the second handler.

Can anyone tell me, what is wrong with these both?

pojazdlockmarker = null 
function unlock (player, key, state) 
        local pojazd = getPedOccupiedVehicle(player) 
        local islocked = isVehicleLocked(pojazd) 
        local x, y, z = getElementPosition(pojazd) 
        local lx = x+5 
                if (islocked == true ) then 
                        pojazdlockmarker = createMarker(lx, y, z, "cylinder", 1, 255, 0, 0, 0) 
            end 
end 
function markerunlock() 
        if getKeyState( "L" ) == true then 
                setVehicleLocked(pojazd, false) 
                outputChatBox ( "Otworzyłeś swój pojazd używając kluczyków!", player, 0, 255, 0 ) 
        else 
                outputChatBox ( "Ten pojazd nie należy do Ciebie!", player, 255, 0, 0 ) 
        end 
end 
addEventHandler("onMarkerHit", pojazdlockmarker, markerunlock) 
addEventHandler("onVehicleStartExit", player, unlock) 

Thanks!

Link to comment

try this

pojazdlockmarker = null 
function unlock (player, key, state) 
        local pojazd = getPedOccupiedVehicle(player) 
        local islocked = isVehicleLocked(pojazd) 
        local x, y, z = getElementPosition(pojazd) 
        local lx = x+5 
                if (islocked == true ) then 
                        pojazdlockmarker = createMarker(lx, y, z, "cylinder", 1, 255, 0, 0, 0) 
            end 
end 
function markerunlock() 
        if getKeyState( "L" ) == true then 
                setVehicleLocked(pojazd, false) 
                outputChatBox ( "Otworzyłeś swój pojazd używając kluczyków!", player, 0, 255, 0 ) 
        else 
                outputChatBox ( "Ten pojazd nie należy do Ciebie!", player, 255, 0, 0 ) 
        end 
end 
addEventHandler("onMarkerHit", pojazdlockmarker, markerunlock) 
addEventHandler("onVehicleStartExit", getRootElement(), unlock) 

Link to comment

Yeah, we've done this before.

Now with this code:

    pojazdlockmarker = null 
    function unlock (player, key, state) 
            local pojazd = getPedOccupiedVehicle(player) 
            local islocked = isVehicleLocked(pojazd) 
            local x, y, z = getElementPosition(pojazd) 
            local lx = x+5 
                    if (islocked == true ) then 
                            pojazdlockmarker = createMarker(lx, y, z, "cylinder", 1, 255, 0, 0, 0) 
                end 
    end 
    function markerunlock() 
            if getKeyState( "L" ) == true then 
                    setVehicleLocked(pojazd, false) 
                    outputChatBox ( "Otworzyłeś swój pojazd używając kluczyków!", player, 0, 255, 0 ) 
            else 
                    outputChatBox ( "Ten pojazd nie należy do Ciebie!", player, 255, 0, 0 ) 
            end 
    end 
    addEventHandler("onMarkerHit", pojazdlockmarker, markerunlock) 
    addEventHandler("onVehicleStartExit", player, unlock) 

we have this error:

  
[15:47:30] WARNING: [gamemodes]/[RPG]/pgs_core/kontrolapojazdu.lua:117: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil] 
[15:47:30] WARNING: [gamemodes]/[RPG]/pgs_core/kontrolapojazdu.lua:118: Bad ament @ 'addEventHandler' [Expected element at argument 2, got nil] 
  

with your code, error is like that:

  
[17:30:53] WARNING: [gamemodes]/[RPG]/pgs_core/kontrolapojazdu.lua:117: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil] 
  

Argument 'GetRootElement()' don't help.

Link to comment
  • Moderators

Hi,

I did some modifications and commented well so you will understand why I did it this way:

-- Client side

function markerToUnlock (player, seat) 
    local vehicle = source 
    if seat == 0 then -- if he was the driver (the passengers won't create the marker) 
        if isVehicleLocked(pojazd) then -- Are you sure you can get out of the vehicle when it is locked ? 
            local x, y, z = getElementPosition(vehicle) 
            local lx = x+5 -- WARNING: it will do +5 on the world's x axis, not the vehicle's x axis and some vehicles will need more or less than +5 
            local pojazdlockmarker = createMarker(lx, y, z, "cylinder", 1, 255, 0, 0, 0) 
            setElementData(pojazdlockmarker, "willUnlockVehicle", vehicle) -- We say that this marker will unlock the vehicle stored in vehicle variable 
            addEventHandler("onClientMarkerHit", pojazdlockmarker, inMarkerUnlockVehicle) -- We say that an onMarkerHit event on pojazdlockmarker will call inMarkerUnlockVehicle function 
            addEventHandler("onClientMarkerLeave", pojazdlockmarker, outMarkerUnlockVehicle) -- We say that an onMarkerHit event on pojazdlockmarker will call outMarkerUnlockVehicle function 
        end 
    end 
end 
addEventHandler("onClientVehicleExit", localPlayer, markerToUnlock) -- you are on the client side so use getLocalPlayer() or localPlayer 
  
function inMarkerUnlockVehicle(hitElement) 
    if hitElement == localPlayer then -- making sure it's the local player 
        setElementData(localPlayer, "isInMarkerUnlock", source) -- We say that the local player is in the marker stored in the source variable 
    end 
end 
  
function outMarkerUnlockVehicle(hitElement) 
    if hitElement == localPlayer then -- making sure it's the local player 
        setElementData(localPlayer, "isInMarkerUnlock", nil) -- We say that the local player is no in a markerUnlock anymore 
    end 
end 
  
function keyUnlock() 
    local markerUnlock = getElementData(localPlayer, "isInMarkerUnlock") 
    if markerUnlock then 
        local vehicle = getElementData(markerUnlock, "willUnlockVehicle") 
        if vehicle then 
            setVehicleLocked(vehicle, false) -- unlock 
            setElementData(localPlayer, "isInMarkerUnlock", nil) -- not in a markerUnlock anymore 
            destroyElement(markerUnlock) -- delete the marker 
            outputChatBox("Otworzyłeś swój pojazd używając kluczyków!", localPlayer, 0, 255, 0) -- message success 
        end 
    end 
end 
bindKey("l", "down", keyUnlock) 

Best regards,

Citizen

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