Jump to content

Getting the nearest vehicle


gerlachmax98

Recommended Posts

This client-sided function should get you the nearest vehicle to the local player.

function getNearestVeicle() 
    local x, y, z = getElementPosition( localPlayer ) 
    local prevDistance  
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 )  then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 

Link to comment
[2013-01-24 20:51:05] WARNING: vio_lite\reparieren\server.lua:85: Bad argument @ 'getElementPosition' [Expected element at argument 1, got nil] 
[2013-01-24 20:51:05] WARNING: vio_lite\reparieren\server.lua:89: Bad argument @ 'getDistanceBetweenPoints3D' 
[2013-01-24 20:51:05] ERROR: vio_lite\reparieren\server.lua:90: attempt to perform arithmetic on local 'distance' (a boolean value) 

I have it now so:

function getNearestVeicle() 
    local x, y, z = getElementPosition( localPlayer ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 )  then 
            prevDistance = distance 
            nearestVehicle = v 
            setElementHealth ( v, 800 ) 
        end 
    end 
    return nearestVehicle or false 
end 
addCommandHandler("repaircar", getNearestVeicle) 

Link to comment

As I said, this function is only for client-side, to use it on server-side, i have to modify it a bit, i will do it later ( cuz im from my phone now ).

And it won't repair your vehicle, it just gets the nearest vehicle to the local player. An example how to use it:

fixVehicle( getNearestVeicle() ) 

I've just noticed that a word is missing from the name of the function, of course it will work with this name too, just doesn't look good :)

Link to comment

Try this:

function getNearestVeicle(thePlayer) 
    local x, y, z = getElementPosition( thePlayer ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 )  then 
            prevDistance = distance 
            nearestVehicle = v 
            setElementHealth ( v, 800 ) 
        end 
    end 
    return nearestVehicle or false 
end 
addCommandHandler("repaircar", getNearestVeicle) 

csiguusz: You're wrong, this function isnt only for client-side, thats for both.

Link to comment

Because i have a lot of time now, a made it! :)

This should work on server-side. But i think it would be better to get the nearest veh on client side, because so many calculations can have an impact on the server's performance.

function getNearestVehicle( player )  
    local x, y, z = getElementPosition( player )  
    local prevDistance  
    local nearestVehicle  
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do  
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) )  
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance  
            nearestVehicle = v 
        end  
    end  
    return nearestVehicle or false  
end 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command 
    if vehicle then 
        fixVehicle( vehicle ) -- fix the nearest vehicle. this is just an example, you can do here anything else 
    end 
end 
addCommandHandler( "repaircar", doSomethingWithTheNearestVeh )  

Edited by Guest
Link to comment

so?

function getNearestVehicle( player ) 
    local x, y, z = getElementPosition( player ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command 
    if vehicle then 
        setPedAnimation( player, "wash_up", "anim") -- set your animation to a player 
        setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds 
        setElementHealth( vehicle, 800 ) -- fix the nearest vehicle. this is just an example, you can do here anything else 
    end 
end 
addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) 

Link to comment
so?
function getNearestVehicle( player ) 
    local x, y, z = getElementPosition( player ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command 
    if vehicle then 
        setPedAnimation( player, "wash_up", "anim") -- set your animation to a player 
        setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds 
        setElementHealth( vehicle, 800 ) -- fix the nearest vehicle. this is just an example, you can do here anything else 
    end 
end 
addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) 

Does it work?

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