Stop methods of modules in LocalScripts

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.

Currently, I have to use a humanoid died event:

Humanoid.Died:Connect(function()
	maid:DoCleaning()
	hitbox:Destroy()
	camShake:Stop()
	controller:Stop()
end)

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:

-- *ON CLIENT*
 ClearBindableEvent.OnServerInvoke:Connect(function()
end)

 ClearBindable.Event:Connect(function()
end)

--> ETC...

Nothing has worked so far, and I’m quite certain this is the source of a memory leak.

2 Likes

Could you tell me what went wrong when trying CharacterRemoving? It should work for both when the player is leaving, and when hes respawning.

1 Like

It literally just does nothing:

Put a LocalScript in a tool and try this code:

local Player = game.Players.LocalPlayer

Player.CharacterRemoving:Connect(function()
	print("Char removed")
end)

I’m pretty sure its because the tool isn’t parented by the time it runs the code, or something along those lines.

1 Like

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)

1 Like

No, they do not disconnect according to the tests I’ve run, and according to this; New Player and Character Destroy Behavior

Again, the problem is detecting if the player has left.

1 Like

Then i can’t suggest anything else other than my first option.

1 Like

I feel that that cannot be the most efficient method, but thank you regardless.

2 Likes

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

1 Like

Because theres connections that will persist, such as runservice, which will leak memory over time

1 Like

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.

1 Like

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.

1 Like

where you require the modules from? if you require them from the local script it gets deleted.

I do, and they are not disconnected. I’ve tested it with multiple modules. The stop functions exist for a reason.

thats weird. it may be a bug within roblox. you could try

humanoid.Died:Connect(function()
    script:Destroy()
end)

or if that does not work (check in a runservice function)

humanoid.Died:Connect(function()
	maid = nil
	hitbox = nil
	camShake = nil
	controller = nil
end)

Ok, I’ll give it a try tomorrow. Again, maybe this has something to do with it?

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?

Yes, I am.

ok i have to get the character minimum now so

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