Need help with GenerateDirectionsToCoord()

I’m working on improvements for my StreetRaces script and I’m trying to figure out how to use the GenerateDirectionsToCoord() native. I created a resource with the following code to generate directions to the current waypoint but for some reason when first running the command it returns 0 for direction and distance but running it a second time gives the correct directions.

-- Test command to generate directions to waypoint
RegisterCommand("directions", function(source, args)
    -- If waypoint is set get directions
    if IsWaypointActive() then
        local waypointCoords = GetBlipInfoIdCoord(GetFirstBlipInfoId(8))
        local retval, coords = GetClosestVehicleNode(waypointCoords.x, waypointCoords.y, 0.0, 0)
        local retval, direction, vehicle, distance = GenerateDirectionsToCoord(coords.x, coords.y, coords.z, 1)
        TriggerEvent('chatMessage', "[TEST]", {0,0,0}, ("Waypoint: %d, %f {%f, %f, %f}"):format(direction, distance, coords.x, coords.y, coords.z))
    end
end)

Adding a 10ms delay and calling GenerateDirectionsToCoord() a second time works however I’m trying to figure out how to make it work without any delay. Ultimately I want to get directions for two different waypoints and then decide which directions to use (basically determine when directions overlap and then use the further checkpoint).

Has anyone experimented with this API or have a code snippet to make it work? I’ve tried a few of the NavMesh and Path API’s to maybe load regions but no success.