Native FindFirstVehicle is too broken

I’m trying to search for a entity in loop that matches the condition Config.Enabled[vehModel] it worked couple days ago and i havent touched it since then but today for no reason it keeps reporting that “carnotfound”. How can I make it more reliable? I am using it to search nearest helicopter model around the player, there is also distance check after target is found :D.

Config.Enabled = {
    ["buzzard2"] = true
}

print("START SEARCHING")

local handle, vehId = FindFirstVehicle()
local target = nil
local success
repeat
    local vehModel = GetDisplayNameFromVehicleModel(GetEntityModel(vehId)):lower()
    if Config.Enabled[vehModel] then
        EndFindVehicle(handle)
        target = vehId
    else
        success, vehId = FindNextVehicle(handle)
    end
until not success

if target then
    print("STOP SEARCHING")
    print("TARGET FOUND")
end

No distance check, No null checks, Handle is not cleaned up, case sens issue maybe?, no validation…

Config.Enabled = { ["buzzard2"] = true }

function FindNearestEnabledVehicle(maxDistance)
    maxDistance = maxDistance or 100.0 -- Default 100 units
    local playerPed = PlayerPedId()
    local playerPos = GetEntityCoords(playerPed)
    
    print("START SEARCHING")
    
    local handle, vehId = FindFirstVehicle()
    local target = nil
    local closestDistance = maxDistance
    
    if handle and vehId then
        repeat
            if DoesEntityExist(vehId) then
                local vehPos = GetEntityCoords(vehId)
                local distance = #(playerPos - vehPos)
                
                if distance <= maxDistance then
                    local vehModel = GetDisplayNameFromVehicleModel(GetEntityModel(vehId))
                    
                    if Config.Enabled[vehModel] or Config.Enabled[vehModel:lower()] then
                        if distance < closestDistance then
                            target = vehId
                            closestDistance = distance
                        end
                    end
                end
            end
            
            local success
            success, vehId = FindNextVehicle(handle)
            if not success then break end
            
        until false
        
        EndFindVehicle(handle)
    end
    
    if target then
        print("STOP SEARCHING")
        print("TARGET FOUND - Distance: " .. string.format("%.2f", closestDistance))
        return target, closestDistance
    else
        print("No enabled vehicles found within range")
        return nil
    end
end

To use
local targetVehicle, distance = FindNearestEnabledVehicle(150.0) – Search within 150 meters or units whatever

Anyhow, not how i would personally do it!

Searching for the nearest vehicle can be done through another native that does not require fiddling with the Find... natives.

-- use the model hashes instead of the name (use `buzzard2` or joaat("buzzard2"))
Config.Enabled = { [`buzzard2`] = true }

function GetClosestHeli(position, radius)
    -- use GetGamePool to get a list of all client side vehicles
    local vehicles = GetGamePool("CVehicle")
    local closestVehicle = nil

    for i = 1, #vehicles do
        -- distance and model check
        local tempDist = #(position - GetEntityCoords(vehicles[i]))
        if (tempDist < radius and Config.Enabled[GetEntityModel(vehicles[i])]) then
            radius = tempDist
            closestVehicle = vehicles[i]
        end
    end

    return closestVehicle
end

-- execution
local closestHeli = GetClosestHeli(GetEntityCoords(PlayerPedId()), 100.0)
if (closestHeli) then
    -- do whatever
end

This cuts down on native calls (and thus improves performance) by a lot compared to using the Find... natives.