MouseLeave isn't triggered when object is tweened away from mouse

Issue:

(If video isn’t playing then right click → open video in new tab)
The frame is tweened and the textbutton never gets tweened back to its regular position when it tweens away from the mouse.

Code used:

local btn = script.Parent

btn.MouseEnter:connect(function()
	btn:TweenPosition(UDim2.new(0,20,0,300),"Out","Quad",0.4,true)
end)

btn.MouseLeave:connect(function()
	btn:TweenPosition(UDim2.new(0,0,0,300),"Out","Quad",0.4,true)
end)

btn.MouseButton1Down:connect(function()
	script.Parent.Parent:TweenPosition(UDim2.new(0,-300,0,0),"Out","Quad",0.4,true)
end)

script.Parent.Parent.back.MouseButton1Down:connect(function()
	script.Parent.Parent:TweenPosition(UDim2.new(0,0,0,0),"Out","Quad",0.4,true)
end)
2 Likes

It’s possible there are two problems here, but one thing I noticed (which could be conflated with your findings) is after you click, MouseLeave won’t fire until MouseEnter fires again. You can fire MouseEnter by moving the mouse one pixel onto the gui.

It’s like the internal state is set to “not hovering” once the user clicks, which means MouseLeave won’t fire until the state is returned to “hovering”.

Can you repeat your experiment with the same gui, but place your mouse over the leftmost 20 pixels of the button? When the gui moves to the right, you can tell if MouseLeave fires at an appropriate time.

Is this what you were asking for?

It sounds like what is going on is that calculations are only completed when the mouse is moved and not when a given instance is moved. This will probably cause certain ‘bugs’ to occur.

  • When a new instance is created beneath the mouse, mouse enter won’t fire.
  • When an instance is moved, the mouse events won’t fire.
  • When an instance beneath the mouse is parented to nil, mouse leave won’t fire.

Fixing this does require more calculations, but I can’t see it being a major overhead.

You could of course look into other ways of designing your buttons, or adding in code to handle these cases. You might have a static button as the parent which you could resize with your visible button/label/frame being anchored to the right. You could also force collapse it when the frame is closed.

Idk if this will be helpful since I don’t know how it works on GUIs, but wasn’t it MouseHoverEnter and MouseHoverLeave?
It was on normal parts at least, GUIs may have different.

No, if that was the case, then my code would error. Those events don’t exist for GUI objects.

@woot3
Yeah I guess ill have to do it myself. UI code is just so tedious.

1 Like