Check which edge of the screen a frame is touching?

Hey, I’m making a dropdown menu UI. I want it so whenever it detects the frame is touching one of the edges of the screen, it moves so that it is now inside the screen.

My current code makes it so if it is fully outside the boundaries of the screen, it will move up above the button the dropdown menu was called from.

local function Position()
        -- MainGui is a ScreenGui. It's AbsoluteSize property shows the size of the screen.
        -- self.Button is the button the dropdown menu is called from
        -- self.Ui is the dropdown menu

        self.Ui.Position = UDim2.new(0, self.Button.AbsolutePosition.X, 0, self.Button.AbsolutePosition.Y + self.Button.AbsoluteSize.Y)

        local a1 = MainGui.AbsolutePosition.Y
        local b1 = self.Ui.AbsolutePosition.Y
        
        local a2 = a1 + MainGui.AbsoluteSize.Y
        local b2 = b1 + self.Ui.AbsoluteSize.Y

        -- check if it is in the bounds of the screen
        if ((b1 <= a1 and b2 <= a1) or (a2 <= b1 and a2 <= b2)) then -- fully out of bounds
            self.Ui.Position = UDim2.new(0, self.Button.AbsolutePosition.X, 0, self.Button.AbsolutePosition.Y - self.Ui.AbsoluteSize.Y)
        end
end

This code only checks if it is FULLY outside the boundaries of the screen. I want to be able to detect if it is AT ALL outside of the boundaries of the screen, and what edge of the screen (top, bottom, left, right) it is outside of.

Hope this makes sense, I’m willing to answer any questions in the replies. Thanks :slight_smile:

1 Like

Just use the size of the UI instance itself.