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
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!
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")
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
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