MouseLeave fires after MouseEnter

function ItemFrame:setupHoverEvent()
	self.frame.MouseEnter:Connect(function()
		print("MouseEnter")
		self:setupHover()
		hovering = true
		popUp.Visible = true
	end)

	self.frame.MouseLeave:Connect(function()
		print("MouseLeave")
		hovering = false
		popUp.Visible = false
	end)
end

Any ideas?

because when the popup appears it blocks the mouse from hovering over the frame and it detects that the mouse left the frame

2 Likes

Would suggest to instead check if the mouse is inside the frame rather than using MouseLeave and MouseEnter events.

1 Like
        -- Get the mouse's current position
        local mousePosition = UserInputService:GetMouseLocation()
        local framePosition = self.frame.AbsolutePosition
        local frameSize = self.frame.AbsoluteSize

        -- Check if the mouse is still inside the frame
        if mousePosition.X >= framePosition.X and mousePosition.X <= framePosition.X + frameSize.X and
           mousePosition.Y >= framePosition.Y and mousePosition.Y <= framePosition.Y + frameSize.Y then
            -- Mouse is still within the frame bounds, don't hide the popUp
            return
        end

I tried that, but I’m not sure if my logic is correct.

Try this:

local Frame = script.Parent
local LocalPlayer = game.Players.LocalPlayer

local function Hovering()
	local MousePos = game:GetService("UserInputService"):GetMouseLocation() - game:GetService("GuiService"):GetGuiInset()
	local Guis = LocalPlayer:WaitForChild("PlayerGui"):GetGuiObjectsAtPosition(MousePos.X, MousePos.Y)
	
	for _, Gui in Guis do
		if Gui == Frame then
			return true
		end
	end
	return false
end

if Hovering() then
	-- do stuff
end
-- Function to check if the mouse is hovering over the frame
local function Hovering(Frame)
	local MousePos = _L.Services.UserInputService:GetMouseLocation() - _L.Services.GuiService:GetGuiInset()
	local Guis = plr.PlayerGui:GetGuiObjectsAtPosition(MousePos.X, MousePos.Y)

	for _, Gui in pairs(Guis) do
		if Gui == Frame then
			return true
		end
	end
end

function ItemFrame:setupHoverEvent()
	-- Connect the InputChanged to continuously check if the mouse is over the frame
	_L.Services.UserInputService.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			if Hovering(self.frame) then
				print("MouseEnter")
				self:setupHover()
				hovering = true
				popUp.Visible = true
			else
				print("MouseLeave")
				hovering = false
				popUp.Visible = false
			end
		end
	end)
end

It works however it returns a lot of false which makes it not work, however it does return true while hovering it

So its basically doing MouseEnter 1x and then MouseLeave 10x simultaneously

Not sure if thats the case because this doesn’t fix it

popUp.Active = false
popUp.Selectable = false

You could try this too:

table.find(PlayerGui:GetGuiObjectsAtPosition(Mouse.X, Mouse.Y), Frame)

it will return true, if you are hovering.

_L.Services.UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local MousePos = _L.Services.UserInputService:GetMouseLocation() - _L.Services.GuiService:GetGuiInset()
		local isHovering = table.find(plr.PlayerGui:GetGuiObjectsAtPosition(MousePos.X, MousePos.Y), self.frame)
		
		if isHovering then
			self:setupHover()
			popUp.Visible = true
		else
			popUp.Visible = false
		end
	end
end)

Yes but even when it returns a non-nil value, after that it returns nil for some reason which makes it invisible

 11:36:07.199   ▶ nil (x18)  -  Client - ItemFrame:32
  11:36:07.219  3  -  Client - ItemFrame:32
  11:36:07.219   ▶ nil (x18)  -  Client - ItemFrame:32
  11:36:07.237  3  -  Client - ItemFrame:32
  11:36:07.237   ▶ nil (x18)  -  Client - ItemFrame:32
  11:36:07.262  3  -  Client - ItemFrame:32
  11:36:07.263   ▶ nil (x18)  -  Client - ItemFrame:32
  11:36:07.283  3  -  Client - ItemFrame:32
  11:36:07.284   ▶ nil (x18)  -  Client - ItemFrame:32
  11:36:07.309  3  -  Client - ItemFrame:32

This is how the output looks whilst hovering on the frame

I think the problem is the one that @CZXPEK mentioned, the popup is covering the frame.

So then woudn’t this fix it? ---- (it didn’t)

1 Like