How do I detect if the mouse is inside a frame?

How do I detect if a players mouse is inside a frame (WITHOUT IGNORING THE ANCHOR POINT)?

Thank you for helping!

1 Like

There are multiple ways to do this, one of which is first by getting the player’s mouse position (on the screen), then you get the frame’s absolute position + size, get the 4 corners, check if he is in between the minX, maxX, minY and maxY
or you can just get a button and use the hover connection or use a gui object and the mouse enter connection:
https://developer.roblox.com/en-us/api-reference/event/GuiObject/MouseEnter

Every GuiObject have their own mouse events, including Frames with MouseEnter. Although these events aren’t really efficient and sometimes not precise, if you’re worried about imperfections someone made a module about these events specifically, to create this functionally apart from the module you can just check the module’s source, the math is pretty simple.

https://devforum.roblox.com/t/fed-up-with-mouseenter-and-mouseleave-not-working-heres-a-module-for-you/155011

Sorry, I was on the right track already, I just didn’t know that AbsolutePosition is always located at Anchor point 0,0.

Thank you for helping anyways, @KJry_s and @Sarchyx

My module ended up looking like this:

function module:MouseBetweenPoints(pointA,pointB)
	local mouseVector = Vector2.new(mouse.X,mouse.Y)
	local pointAVector = Vector2.new(pointA.X.Offset,pointA.Y.Offset)
	local pointBVector = Vector2.new(pointB.X.Offset,pointB.Y.Offset)
	return ((mouseVector.X > pointAVector.X and mouseVector.Y > pointAVector.Y) and (mouseVector.X < pointBVector.X and mouseVector.Y < pointBVector.Y))
end

function module:MouseInFrame(frame)
	local pointAVector = frame.AbsolutePosition
	local pointBVector = frame.AbsolutePosition + frame.AbsoluteSize
	return module:MouseBetweenPoints(UDim2.fromOffset(pointAVector.X,pointAVector.Y),UDim2.fromOffset(pointBVector.X,pointBVector.Y))
end
3 Likes