Building system snapping issue

Hello so im currently trying to make a building system kinda like rust or fortnite where you place a floor and then the walls automatically snap to that floor. The walls have attachment points on the side to place the walls when you hover over them. My issue is its only going to one point

Heres what I tried, I loop through pairs of foundation (so the attachment points) and then check the distance the mouse is from the point. Everything prints right, the distance, the names all that. But for some reason after locking to a point it never wants to lock onto another point again always the same point over and over.

RunService.RenderStepped:Connect(function()
    if Mouse.Target == game.Workspace.Foundation then
        for i,attach in pairs(game.Workspace.Foundation:GetChildren()) do
            local distance = (attach.WorldPosition -  Mouse.Hit.Position).magnitude
            if distance < 3 then
                Wall.Position = Vector3.new(attach.WorldPosition.X, attach.WorldPosition.Y + 4.5, attach.WorldPosition.Z)
            else
                Wall.Position = Mouse.Hit.Position
                Wall.Position = Vector3.new(Wall.Position.X, Wall.Position.Y + 4.5, Wall.Position.Z)
            end
        end
    else
        Wall.Position = Mouse.Hit.Position
        Wall.Position = Vector3.new(Wall.Position.X, Wall.Position.Y + 4.5, Wall.Position.Z)
    end
end)

It does not find the closest point, but instead it gets the last point in the pairs loop and places it there