Stop methods of modules in LocalScripts

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

How did you watch the output when you left the game?

Server viewer- i tested it in studio

What do you mean by server viewer? Are you trying it in a play solo session and you’re switching to the server? If so, your player is not removed from the game when you do this, you’re just switching to what the server “sees”. You’d be able to see what the client outputs iirc

Would this be necessary because the humanoid.died connection would be in a localscript parented to a tool

Yeah because the character isn’t destroyed when the character dies, just parented to nil afaik, you can try it though by printing connection.Connected

Does this mean connections like uis.inputbegan should also be disconnected? Ive been told connections in tools are automatically disconnected when the tool is destroyed

I had to test it to be 100% certain, but no, your connections won’t be disconnected.

image

Very lazy testing code inside StarterPlayerScripts (don’t use this unless you want to test to see for yourself):

game:GetService('Players').LocalPlayer.CharacterAdded:Connect(function()
	print('character was added')
	print('Is old humanoid.Died active:', _G.last and _G.last.Connected)
	print('Is old script destroyed:', not pcall(function()
		_G.lastScript.Parent = workspace
	end))
end)

Tool:

while not script:IsDescendantOf(workspace) do
	script.AncestryChanged:Wait()
end
_G.last = script.Parent.Parent:WaitForChild('Humanoid').Died:Connect(function() end)
_G.lastScript = script

Wtf :man_facepalming::man_facepalming: Im so cooked; my memory leak is probably from my client code connections; then?

If you have them inside tools or in the character and aren’t disconnecting them, then it very well could be, yeah.

1 Like