Jump to content

The most weird issue with cancelEvent()


myonlake

Recommended Posts

  • MTA Team

Hey,

For some reason the below code doesn't work like how I want it to. I have done this many times and this time it doesn't really work with me. Probably a silly typo or something in the script.

Client-side

bindKey("K", "down", 
    function() 
        local vehicle = getPedOccupiedVehicle(localPlayer) 
        if vehicle then 
            if getVehicleController(vehicle) == localPlayer then 
                setVehicleLocked(vehicle, not isVehicleLocked(vehicle)) 
                outputChatBox("Vehicle " .. (isVehicleLocked(vehicle) and "" or "un") .. "locked.", 255, 180, 0, false) 
            end 
        end 
    end 
) 
 

Server-side

addEventHandler("onVehicleStartExit", root, 
    function(player, seat, jacked, door) 
        if isVehicleLocked(source) then 
            cancelEvent() 
            outputChatBox("The door is locked.", player, 255, 0, 0, false) 
        end 
    end 
) 
  
addEventHandler("onVehicleEnter", root, 
    function(player, seat, jacked) 
        if not isVehicleLocked(source) then 
            if tonumber(getElementData(source, "vehicle.owner")) == 0 then 
                outputChatBox("This " .. getVehicleName(source) .. " is a civilian vehicle.", player, 255, 180, 0, false) 
            elseif not tonumber(getElementData(source, "vehicle.owner")) then 
                outputChatBox("This " .. getVehicleName(source) .. " belongs to " .. getElementData(source, "vehicle.owner"):gsub("_", " ") .. ".", player, 255, 180, 0, false) 
            end 
        else 
            local x, y, z = getElementPosition(player) 
            removePedFromVehicle(player) 
            setElementPosition(player, x, y, z + 2) 
        end 
    end 
) 
  
 
Edited by myonlake
Link to comment

Try this

c:

bindKey("K", "down", 
    function() 
        local vehicle = getPedOccupiedVehicle(localPlayer) 
        if vehicle then 
            if getVehicleController(vehicle) == localPlayer then 
                triggerServerEvent("lockCar", localPlayer, vehicle) 
            end 
        end 
    end 
) 

s:

addEventHandler("onVehicleStartExit", root, 
    function(player, seat, jacked, door) 
        if isVehicleLocked(source) then 
            cancelEvent() 
            outputChatBox("The door is locked.", player, 255, 0, 0, false) 
        end 
    end 
) 
  
addEventHandler("onVehicleEnter", root, 
    function(player, seat, jacked) 
        if not isVehicleLocked(source) then 
            if tonumber(getElementData(source, "vehicle.owner")) == 0 then 
                outputChatBox("This " .. getVehicleName(source) .. " is a civilian vehicle.", player, 255, 180, 0, false) 
            elseif not tonumber(getElementData(source, "vehicle.owner")) then 
                outputChatBox("This " .. getVehicleName(source) .. " belongs to " .. getElementData(source, "vehicle.owner"):gsub("_", " ") .. ".", player, 255, 180, 0, false) 
            end 
        else 
            local x, y, z = getElementPosition(player) 
            removePedFromVehicle(player) 
            setElementPosition(player, x, y, z + 2) 
        end 
    end 
) 
  
addEvent("lockCar", true) 
addEventHandler("lockCar", root, function(c) 
     setVehicleLocked(c, not isVehicleLocked(c)) 
     outputChatBox("Vehicle " .. (isVehicleLocked(c) and "" or "un") .. "locked.", source, 255, 180, 0, false) 
end ) 

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