Issues with mouse enter and mouse leave

I’m trying to make some cards go up when a mouse enters them and go down when a mouse leaves them. Though due to what I assume is the fault of unreliable mouse leave, the cards sometimes “lag” and stay in place even when mouse leave is triggered.

At first I looked this issue up and found a dev forum post about it. I tried one of the fixes mentioned, which is to wait a frame after mouse enter or mouse leave is triggered, which didn’t work, Then I tried using a module which should have fixed said issue, but alas it did not work.

Any kind of help will be greatly appreciated

Here’s the code:

also the reason I added an if statement for looking is to stop the player from selecting multiple cards at the same time, even when said cards were behind each other

local OldPos = CardC.Position
   			
CardC.MouseEnter:Connect(function()
    				
   if Looking == false  then
   	Looking = true
   				
   	CardC.ZIndex+=1


   	Tween(CardC,TweenInfo.new(0.05),{Position =OldPos+UDim2.new(0,0,0,-40)})

   end
end)

CardC.MouseLeave:Connect(function()
   			
   if Looking == true then
   	Looking = false
   				
   	CardC.ZIndex-=1

   	Tween(CardC,TweenInfo.new(0.05),{Position =OldPos })


   end
end)

Mouse enter and mouse leave should never be used.

Here is an open sourced module that solves the unreliability of these events.

1 Like

I tried using that module, didn’t help at all

It did not help because you did not use it properly. Give me one second and i’ll make it work.

local MouseOverModule = require(game.ReplicatedStorage.MouseOverModule)

local MouseEnter, MouseLeave = MouseOverModule.MouseEnterLeaveEvent(CardC)

local OldPos = CardC.Position

MouseEnter:Connect(function()
	if Looking == false  then
		Looking = true

		CardC.ZIndex+=1

		Tween(CardC,TweenInfo.new(0.05),{Position =OldPos+UDim2.new(0,0,0,-40)})

	end
end)

MouseLeave:Connect(function()
	if Looking == true then
		Looking = false

		CardC.ZIndex-=1

		Tween(CardC,TweenInfo.new(0.05),{Position =OldPos })

	end
end)

Make a module in replicatedstorage called MouseOverModule and include the module code. Then try replacing your script with this script.

That’s the exact same code I used when trying to get it to work

Does it give you any errors? Could you try doing some print tests and seeing where the code stops?

apparently I was doing it wrong, because it works after I remade the code to work with the module. Weird

1 Like