Question about MouseEnter, MouseLeave events

I have some GUI elements that popup a window of information and a window with buttons that need to be pressed to use the Item, in the case of various inventory listings. As I have these

MouseEnter:Connect(function() --[[ code here ]]-- end)

MouseLeave:Connect(function() --[[ code here ]]-- end)

Should I be tracking every element that these get the Connect event applied to and be disconnecting the event from them, before destroying them? I’m encountering a lot of lag in my new development and I’m working to eliminate as much of this, as possible.

1 Like

You could make a custom button script. What I mean is that you can get the mouse position when the player clicks and check which button is being pressed knowing its position and size. This way, you would only have to connect a single event. However, it would be tedious to make a hovering effect. Note that MouseEnter and MouseLeave are not quite reliable since they fail too often…

By the way, if you are willing to destroy those buttons when they are no longer needed, you don’t have to worry about events connected to them. When you call Destroy(), you are inherently disconnecting all the instance’s connections as well as those of its children.

In order to enhace performance, you could try optimizing your scripts. This thread might help. Also, having numerous unanchored parts or too many collisions can drop fps.

[color=orange]Edit: thanks for ignoring[/color] :sob:

1 Like

Calling :Destroy() on an object disconnects all events. I still track individual events, but it’s not strictly necessary.

What are you doing inside the loop? If you’re resizing or repositioning the element you might be inadvertently triggering the event multiple times.

Yeah, I’ve had to put in a lot of debounce flags in my building loops… where I have the MouseEnter, MouseLeave events, they fire on GUI elements, only - popping up a window as I hover over an inventory item, and the window goes away either automatically or as the mouse leaves the frame. I was using Destroy, and then recloning GUI templates as needed to have events reapplied, and thought about setting up an array in _G. to track the events as well, but I may be overcomplicating things. One thing I should make sure I’m doing is not readding events to elements already initialized - I imagine its possible to do this… Thanks for your help.