After reading a bunch of previous posts on the DevForum regarding memory leaks, I’m still a bit confused if this is what’s actually causing the problems with ServerScript delays.
If I was to have a ServerScript use :Connect() when a ClickDetector is used with MouseClick, but never click the Ancestor Part the first place, will this still cause lag in my game over time…even if I’m never “Connecting” to the function?
In other words, will this cause lag eventually over time even if I don’t click the Part:
The literal definition of a memory leak is when a program doesn’t remove unnecessary data from memory, which implies that there is useless data stored in memory. If the click detector is never being used, it fits that criteria where it hogs memory despite not being used at all.
Yeah, unless you’re going to add something with function that connects to that, it’s rendered absolutely useless and better to remove it rather than leave it.
technically, this is a “memory leak” if you dont click it again. HOWEVER: For simpler applications like this the memory consumed is superficial, and therefore it would be acceptable to leave it connected if you only have one click detector. Like youre not gunna disconnect it when there are no players in range, then reconnect it when players come into range. At that point your spending more processing power to do that then you would memory by just leaving the connection.
Another thing that I saw on the OP was that you stated, “In other words, will this cause lag eventually over time even if I don’t click the Part:” I want to clarify that the memory consumed here will not increase over time, as the amount of connections doesnt change. When you connect it originally, your memory increases. If/when you disconnect it, the memory will decrease. By leaving it connected, the memory will not increase over time. It will just increase once when you connect to the signal.
So my overall advice here is that it depends on the application. For example, if you literally want a click detector to only be clicked once, disconnecting it would be a logical thing to do (or just destroying the click detector will also work) because this would prevent use of extra memory. As opposed to something like having a sectioned off world, where only one section is open at once. in this case, unless you have 100s of click detectors in each section there will not be any noticeable benefit from disconnecting inaccessible click detectors.
That was a lot of explaining, so lmk if you want clarification anywhere.
If you just have one click detector that is never going to be used, the lag impact should be pretty neglegible. Having the script and click detector exist won’t hurt, but is good practice to avoid redundant useless scripts that are always active.
Yup. The reason we want to disconnect the events is so they canbe garbage collected. However, roblox likes to help us out. So if we destroy an instance, any connections to rbxscriptsignals of that instance are also lines up for GC automatically. So if youre only using the click detector once, I would just destroy it when your done to clean everything up in one line of code