Jump to content

Prevent car running over players


Chaos

Recommended Posts

So i should use this with onPlayerDamage or which event ?

function disableVehicleCollisionsNearPlayer(thePlayer, maxDistance) 
    local playerX, playerY, playerZ = getElementPosition(thePlayer) 
    local vehicles = getElementsByType("vehicle") 
    for k,v in ipairs(vehicles) do 
        local vehicleX, vehicleY, vehicleZ = getElementPosition(v) 
        -- get the distance between the player and the vehicle: 
        local distance = getDistanceBetweenPoints3D(vehicleX, vehicleY, vehicleZ, playerX, playerY, playerZ) 
        if (distance <= maxDistance) then 
            -- disable collisions for the vehicle 
            setElementCollisionsEnabled(v, false) 
        end 
    end 
end 
  

Link to comment

it's not working correctly

    function enterVehicle ( thePlayer, seat, jacked ) 
        local playerX, playerY, playerZ = getElementPosition(thePlayer) 
        local vehicles = getElementsByType("vehicle") 
        for k,v in ipairs(vehicles) do 
            local vehicleX, vehicleY, vehicleZ = getElementPosition(v) 
            -- get the distance between the player and the vehicle: 
            local distance = getDistanceBetweenPoints3D(vehicleX, vehicleY, vehicleZ, playerX, playerY, playerZ) 
            if (distance <= 1) then 
                -- disable collisions for the vehicle 
                setElementCollisionsEnabled(v, false) 
            end 
        end 
    end 
    addEventHandler ( "onVehicleEnter", getRootElement(), enterVehicle ) 

Link to comment
it's not working correctly
    function enterVehicle ( thePlayer, seat, jacked ) 
        local playerX, playerY, playerZ = getElementPosition(thePlayer) 
        local vehicles = getElementsByType("vehicle") 
        for k,v in ipairs(vehicles) do 
            local vehicleX, vehicleY, vehicleZ = getElementPosition(v) 
            -- get the distance between the player and the vehicle: 
            local distance = getDistanceBetweenPoints3D(vehicleX, vehicleY, vehicleZ, playerX, playerY, playerZ) 
            if (distance <= 1) then 
                -- disable collisions for the vehicle 
                setElementCollisionsEnabled(v, false) 
            end 
        end 
    end 
    addEventHandler ( "onVehicleEnter", getRootElement(), enterVehicle ) 

Making it easy, this is client-sided.. on entering vehicle sets it disabled to collide with players, and on exit reverse it.

function enterVehicle() 
    local players = getElementsByType ( "player" ) 
    setElementCollidableWith ( source, players, false ) 
end 
addEventHandler ( "onClientVehicleEnter", getRootElement(), enterVehicle ) 
  
function exitingVehicle () 
    local players = getElementsByType ( "player" ) 
    setElementCollidableWith ( source, players, true ) 
addEventHandler("onClientVehicleStartExit", getRootElement(), exitingVehicle) 

Link to comment
well, i don't want ghost mode i just want to cancel it

ah, thats weird, you have to use:

onClientVehicleCollision 

and do something like on hitting element player, bouncing back the vehicle or setting delay by freezing vehicle!

cancelEvent() will not work!

Link to comment

Try this.

addEventHandler("onClientPlayerDamage", localPlayer, 
    function(theAttacker, theWeapon, theBodypart) 
        if ( theAttacker.type == "vehicle" and localPlayer.vehicle ~= theAttacker ) then 
            cancelEvent() 
        end 
    end 
) 

You can also use OnClientPreRender and run a quick check to see if a player has collided with the vehicle and quickly allow it to pass through

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