Trouble With Detecting when mouse is within a frame Via Absolute Position & Size

so im trying to do something to detect when a mouse is in a gui, kinda like mouse enter and leave connections

but ofc im not using them because they are insanely buggy

i tried making some code and it doesnt exactly work

local function HoverCheckFunction()
	coroutine.resume(coroutine.create(function()
		while task.wait() do
			local Mx = Mouse.X
			local My = Mouse.Y

			local HoverCheckCoroutine = coroutine.create(function()
				for _NUM, Button in ipairs(ButtonsTable) do
					local Bx = Button.AbsolutePosition.X
					local By = Button.AbsolutePosition.Y
					local Bsx = Button.AbsoluteSize.X
					local Bsy = Button.AbsoluteSize.Y

					local xCheck = false
					local yCheck = false

					if Bx-(Bsx/2) < Mx and Mx < Bx+(Bsx/2) then xCheck = true end
					if By-(Bsy/2) < My and My < By+(Bsy/2) then yCheck = true end
					
					print(Button.Name, if xCheck then "XCHECK WOKRED" else "XCHECK FAILED", if yCheck then "YCHECK WORKED" else "YCHECK FAILED")
					if xCheck and yCheck then
						ButtonGlowEffect(Button)
					end
				end
			end)

			coroutine.resume(HoverCheckCoroutine)
		end
	end))
end 

and shown here it doesnt exactly work as planned.

External Media

unsure what i did wrong but any help would be nice

1 Like

Could still use some help on this

Let me start off by asking why you say that MouseEnter and MouseLeave are considered buggy? It must be your case of use that I am failing to see a practical reason. I highly recommend using the MouseEnter and MouseLeave events before using this, as doing a frame by frame can consume unecessary CPU. But nonetheless, here is your solution:

The part that is messed up in your equation is the Bsx and Bsy portion. This is due to the position of a UI object, unlike Vectors in a part for example, being in the top left corner. so your prefered equation would be

Bx < Mx and Mx < Bx+(Bsx)
By < My and My < By+(Bsy)

However: Roblox alredy does it for you! :star_struck:

Using the PlayerGui from the player which you can get by doing

local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")

you can run the method GetGuiObjectsAtPosition to grab a table of all of the GUI elements within the mouse position. We do this as so:

local guiObjects = PlayerGui:GetGuiObjectsAtPosition(Mx, My)
for _, gui in guiObjects do
     if gui == Button then
          print(Button.Name, "XCHECK WORKED")
          ButtonGlowEffect(Button)
          return
          -- return halts all further code from being run
     end
end
return print(Button.Name, "XCHECK FAILED")

And there you go!

2 Likes

Absolute posi is top left corner not the middle of the frame. So your code should look like that:

local function HoverCheckFunction()
	coroutine.resume(coroutine.create(function()
		while task.wait() do
			local Mx = Mouse.X
			local My = Mouse.Y

			local HoverCheckCoroutine = coroutine.create(function()
				for _NUM, Button in ipairs(ButtonsTable) do
					local Bx = Button.AbsolutePosition.X
					local By = Button.AbsolutePosition.Y
					local Bsx = Button.AbsoluteSize.X
					local Bsy = Button.AbsoluteSize.Y

					local xCheck = false
					local yCheck = false

					if Bx < Mx and Mx < Bx + Bsx then xCheck = true end
					if  By < My and My < By+ Bsy then yCheck = true end
					
					print(Button.Name, if xCheck then "XCHECK WOKRED" else "XCHECK FAILED", if yCheck then "YCHECK WORKED" else "YCHECK FAILED")
					if xCheck and yCheck then
						ButtonGlowEffect(Button)
					end
				end
			end)

			coroutine.resume(HoverCheckCoroutine)
		end
	end))
end

I recomend using this if not mouse enter and mouse leave

Does it not change if i set the anchor point to .5, .5?

Havent tried it in a bit so it may be fixxed? but if you move your mouse just ever so slightly to fast or exit to hastely after entering, it doesnt detect that the mouse left / enterted in the first place

DIdnt know that GetGuiObjectsAtPosition existed, seems to be alot easier to work with, ill go try it out

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.