Ok, I’ve been making a lot of posts on optimization/memory leaks, and here I go again.
I have a LocalScript parented to a tool. In this LocalScript, I construct and initiate several modules; for instance, a hitbox module and a custom shiftlock module.
When a character is removed (whether by dying, or by leaving the game, or really any other situation), I need to call the stop methods of these modules.
However, of course this does not work when a player leaves or is removed some other way. I’ve tried:
Tool.Destroying
Tool:GetPropertyChangedSignal("Parent"):Connect(function()
if not Tool Parent then
end
end)
Tool.AncestryChanged
--> And a host of other things, like CharacterRemoving, etc
I’ve also tried to detect when a player dies in ServerScriptService, using both a RemoteEvent and a BindableEvent. Something like this:
You are correct, that doesn’t work. For resetting you can just use humanoid.Died as you said, for leaving, you could do something like: turn your localscript inside the tool into a modulescript, make a Destroy function in it, detect player leaving in a different localscript (not inside characterscripts), require the modulescript from the localscript, and once the player is leaving just call the Destroy method, it’s not the best way to do it but i can’t think of any other idea.
EDIT: I’m not even sure if they have to be disconnected, shouldn’t they be automatically when the player leaves?!
EDIT 2: You could make a new maid handler, give a function to it, which just disconnects everything you have to, and it should disconnect them when the sword is deleted, including leaving?! (haven’t tested it just a guess)
I might be missing something but why are you trying to clean up memory on the client that’s leaving? Once the client leaves the game, all the memory associated with the game is freed on that client’s machine
he is saying there’s no reason to remove any modules when the client leaves because roblox already removes all of the script data when the player leaves. however when the player dies you should not have to remove the modules manually if the script is under the player’s character because the script itself gets deleted, which removes connections.
No. If I’m using a hitbox module that uses RunService, which is initiated on the construction function, this RunService connection will not disconnect when a character disconnects/dies.
interesting article. i did not know characters weren’t destroyed properly whenever they died. it may have something to do with it, however i am pretty sure that the client properly disconnects everything whenever you leave so you shouldn’t have to worry about memory leaks for when they leave.
Okay but you mentioned that listening to humanoid.Died will clean up the connections when the player dies. This should be the only thing you have to worry about on the client.
When the player leaves, nothing related to the game, on the client that left, will remain.
Just to clarify, you are asking about removing the connections that are made on the client, when that client leaves, right?
Ok, then this can’t be a source of a memory leak because as I said before, everything on the client is disposed of when the client leaves the game. Not disconnecting client-created connections/destroying client-created objects upon the player being removed removed from the game, will not leak memory on the client’s machine.
Are you creating connections to listen to other players’ events? That will leak memory unless you disconnect them.
If not, and you’re only talking about connections created by the client, related to the client’s own player, then I’m a bit lost. Can you elaborate on how you’re coming to the conclusion that this is the source of a memory leak, ie what you’re monitoring that shows steady memory increase, etc, and never going down? Because to my knowledge, especially given how heavily Roblox is sandboxed, it is not possible to have a memory leak (or even have memory keep being reserved) even when the client leaves the game.
On a side note, it might also be a good idea to disconnect your humanoid.Died connection (or use humanoid.Died:Once)
I just added a print statement to one of the runservice connections, then left the game, and watched the output and the print statements didnt cease.
Ill test it again, and ill look for other sources of memory leaks in my code.
I’ve been told that its bad practice to use maid to manage native connections, such as CharacterAdded, so maybe thats the problem