How to check if frames are touching

Hi,
I need a function that would check if a frame is touching another frame and return true if so. One frame is a pin (I’m doing a lockpicking system) and the other one is a centre line. Both are sized and positioned using Scale not Offset.

I got this function from one nice person but it doesn’t seem to work, it throws an error about comparing UDim2 > UDim2.

function:

local function isLockTouchingCenter(lock)
	local lockPos, lockSize = lock.Position, lock.Size
	local centerPos, centerSize = centerLine.Position, centerLine.Size

	if lockPos + lockSize > centerPos and lockPos < centerPos + centerSize then
		return true -- lock is touching the center line
	end
end
1 Like

You’d have to use the AbsoluteSize and AbsolutePosition properties, note that this does not take into account or anchorpoints or rotation but this should give you a basic idea since PlayerGuis have a :GetGuiObjectsAtPosition method:

local function getTouchingFrames(frame)
    local corners = { 
        frame.AbsolutePosition
        frame.AbsolutePosition + Vector2.new(0, frame.AbsoluteSize.Y)
        frame.AbsolutePosition + Vector2.new(0, frame.AbsoluteSize.Y);
        frame.AbsolutePosition + frame.AbsoluteSize;
    }
    local touching = {}
    for i,position in ipairs(corners) do
        local guiObjects = playerGui:GetGuiObjectsAtPosition(position)
        for i,object in ipairs(guiObjects) do
            if object ~= frame then
                table.insert(touching,object)
            end
        end
    end
    return touching
end
1 Like

Thank you for helping, I’ve already done it another way and it works just fine.

1 Like