Jump to content

[Solved] How to cancel the action of entering a vehicle without cancelEvent?


Recommended Posts

I need to be able to cancel at any time during the execution of entering a vehicle.

My problem is the following, there is a collision behind the vehicle, and when the player presses to enter a vehicle and presses the actionKey, he should be attached to the vehicle, but as he is performing the action of entering the vehicle this does not happen, and the problem with this is that the player is left without colliding with the vehicle.


Bug demo:

https://youtu.be/um9erpGCSzE

Client.lua
 

bindKey(actionKey, "down", function()
    if playerInColshape then
        local elementData = getElementData(playerInColshape, "waste_collector")
        
        if elementData and elementData.owner == localPlayer then
            local vehicle = getElementParent(playerInColshape)
            setElementCollidableWith(localPlayer, vehicle, false)
            triggerServerEvent("onPlayerRequestAttach", localPlayer, playerInColshape)
        end
    end
end)


Server.lua

 

addEvent("onPlayerRequestAttach", true)
addEventHandler("onPlayerRequestAttach", root, function(colshape)
    local player = source
    local vehicle = getElementParent(colshape)
    
    if attachedPlayers[player] then
        detachPlayerFromVehicle(player)
        togglePlayerControls(player, true)
    else
        attachPlayerToVehicle(player, vehicle)
        togglePlayerControls(player, false)
    end
end)

 

Edited by Gaimo
Link to comment
  • Gaimo changed the title to [Solved] How to cancel the action of entering a vehicle without cancelEvent?

I used setElementPosition as androski mentioned on discord and a setTimer and it worked.
 

bindKey(actionKey, "down", function()
    if playerInColshape then
        local elementData = getElementData(playerInColshape, "waste_collector")
        
        if elementData and elementData.owner == localPlayer then
            local vehicle = getElementParent(playerInColshape)
            local x, y, z = getElementPosition(localPlayer)
            setElementPosition(localPlayer, x, y, z) 

            setTimer(function()
                setElementCollidableWith(localPlayer, vehicle, false)
                triggerServerEvent("onPlayerRequestAttach", localPlayer, playerInColshape)
            end, 50, 1)
        end
    end
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...