Ledge Grab System

I have this in a local script I need to get a table of the clostestpoint/closestpoints and then get the highest index out of them to move a player to a ledge. This is for a ledge grab system.

local RaycastsTable = {}
local AnchorsTable = {}

for i=1, 15 do
    local AnchorPoint = Instance.new("Part")
    AnchorPoint.TopSurface = Enum.SurfaceType.Smooth
    AnchorPoint.BottomSurface = Enum.SurfaceType.Smooth
    AnchorPoint.Transparency = 0.5
    AnchorPoint.Size = Vector3.new(.25,.25,.25)
    AnchorPoint.Shape = Enum.PartType.Ball
    AnchorPoint.Color = Color3.new(0, 1, 1)
    AnchorPoint.Anchored = true
    AnchorPoint.CanCollide = false
    AnchorPoint.Parent = workspace
    
    local NewRaycastParams = RaycastParams.new()
    NewRaycastParams.FilterDescendantsInstances = { Character, AnchorPoint }
    
    table.insert(RaycastsTable,NewRaycastParams)
    table.insert(AnchorsTable,AnchorPoint)
end

local AnchorYPoint = Instance.new("Part")
AnchorYPoint.TopSurface = Enum.SurfaceType.Smooth
AnchorYPoint.BottomSurface = Enum.SurfaceType.Smooth
AnchorYPoint.Transparency = 0.5
AnchorYPoint.Size = Vector3.new(.25,.25,.25)
AnchorYPoint.Shape = Enum.PartType.Ball
AnchorYPoint.Color = Color3.new(0, 1, 1)
AnchorYPoint.Anchored = true
AnchorYPoint.CanCollide = false
AnchorYPoint.Parent = workspace

local RaycastResults = {}

local function FindClostestAnchors(RayscastResults)
    local ClosestAnchors = {}
    for i, Result in pairs(RaycastResults) do
        if i > 1 then
            if (Result.Position-Character.PrimaryPart.Position).Magnitude < (RayscastResults[i].Position-Character.PrimaryPart.Position).Magnitude then
                
            end
        end
    end
    
    return ClosestAnchors
end

RunService.Heartbeat:Connect(function()
    RaycastResults = {}
    for i, RaycastParam in pairs(RaycastsTable) do
        if RaycastParam then
            local RaycastResult = workspace:Raycast(Vector3.new(Character.PrimaryPart.Position.X,(Character.PrimaryPart.Position.Y-(Character.PrimaryPart.Size.Y*1.75))+(i/2),Character.PrimaryPart.Position.Z), Character.PrimaryPart.CFrame.LookVector * 10,RaycastParam)
            table.insert(RaycastResults,RaycastResult)
            if RaycastResult then
                AnchorsTable[i].Position = RaycastResult.Position
            else
                AnchorsTable[i].Position = Vector3.new(Character.PrimaryPart.Position.X,(Character.PrimaryPart.Position.Y-(Character.PrimaryPart.Size.Y*1.75))+(i/2),Character.PrimaryPart.Position.Z)
            end
        end
    end
    
    local ClostestAnchors = FindClostestAnchors(RaycastResults)
    
end)

My current code

The AnchorYPoint is gonna have a raycast too and that way I can find the top of the object that is being scanned

https://i.gyazo.com/6e7ef9fecb7fd380201d8227da5507d5.mp4