ScriptingExamples/EnterVehicleAsPassenger: Difference between revisions

From GTA Connected
Jump to navigation Jump to search
No edit summary
 
(No difference)

Latest revision as of 18:30, 5 February 2019

Description

Enter the nearest vehicle as a passenger (Default distance = 5)

Code

Lua, Client-Side, GTAC versions 1.1.10 and newer:

function getNearestVehicleInRange(position, distance)
    distance = distance or 5
    for index, vehicle in pairs(getVehicles()) do
        if position:distance(vehicle.position) <= distance then
            return vehicle
        end
    end
    return nil
end

addEventHandler("OnKeyDown", function (event, keyCode, scanCode, mod) 
    if keyCode == SDLK_g then
        if not localPlayer.vehicle then
            local v = getNearestVehicleInRange(localPlayer.position)
            if v ~= nil then
                localPlayer:enterVehicle(v, false)            
            end
        end
    end
end)