Draggable UI Inventory System Similar to Apocolypse Rising 2

Please forgive me if any of my topic formatting is wonky compared to what you normally see as this is my first post on the dev forum.

What I’m currently working on
wrello’s Place Number: 20 - Roblox Studio (gyazo.com)

How it works
I’m using the draggable object module made by someone else found here: Simple Module For Creating Draggable GUI Elements

Before anything it creates a table of start and end points (top left and bottom right) for all the current draggable frames.

  1. When the frame is lifted it destroys the old start and end points and marks that frames position.

  2. Then it checks to see if that frame’s top left corner is within the grid.

  3. Then it checks to see if that frame’s top left or right or bottom left or right is within any of the other draggable frames (using a for loop)
    If it is then it goes back to the marked position.

  4. If it passes both of those checks (it’s in the grid and not inside another frame) then I snap it (the top left corner) to the closest interval of 50 between the grid.AbsolutePosition.X.Offset and the grid.AbsolutePosition.X.Offset + grid.AbsoluteSize.X and the same thing for the Y axis.

  5. Once it’s in the new position I go through the table of top left and bottom right positions and remove this frame’s old positions and replace it with the frames new positions.

The problem
I don’t know how to make it so even if the frame that’s being dragged does end up being inside of another frame then it won’t reset it’s position, but rather go to the closest available interval (snap to position). How would I get the closest available interval if all the frame sizes differ?