Character Module Script still running after reset

I have a module script that is in StarterCharacterScripts (so it is destroyed and recreated each time a character resets)

This module, detects the ‘Shift’ key to run. I added a print saying “Running”

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		print("Running")
	end
end)

So when I first log in, I hit Shift and I see “Running”

if I reset character, and then hold shift, I see…
“Running”
“Running”

if I reset again, and hold shift I get
“Running”
“Running”
“Running”
and so on

I have not had (or not noticed) this ever happening before this summer. So not sure if Roblox has changed something.

So what would cause this sort of behavior?
Could it be that I have a reference to the ‘require’ of this module somewhere stored, would that cause this?

If so, what would be an easy way to find the require reference and would I just set it to ‘nil’?

Also, could it be that the UserInputService.InputBegan:Connect is not being cleaned up when the Character resets?
Should I manually :Destroy all module scripts in my client upon CharacterResetting, to make sure the Connections are destroyed?

you could disable the script that is requiring the module.

I would change this to a StarterPlayer script so it doesnt get reset. And use a RBXConnection to enable and disable it.
https://developer.roblox.com/en-us/api-reference/datatype/RBXScriptConnection

1 Like

you didnt disconnect the past event you’ve created

this is what happens

player spawns    --event 1 created and is working
player dies
player character added --event 2 created and is working


you see how the first event is still working?
you have to use event:Disconnect()

just a note

think of creating events as creating a thread

a script = a thread

a thread can create more threads through the use of coroutine or roblox connection event

by connecting a user input event, you are basically making another script or more accurately a thread

because you’ve never told the computer to stop making more threads or more accurately you’ve never told the computer to destroy user input events which results in threads running in the background even tho their not needed. This is what we call a memory leak and a memory leak is exactly what your experiencing here

you should really put your script in playerScripts

1 Like

I was always told that if the script that created the event is destroyed, the events are disconnected.
Maybe this doesnt’ work the same with module scripts?

“if the script that created the event is destroyed, the events are disconnected”
actually this is false

i’ve tested it myself

this is what i did

put a “Script” in workspace

code

while wait() do --do not actually use while wait do in practice
    print"i am here"
end

script:Destroy()

and if the loop keeps running, that means the game has destroyed the [script object] but the game has not destroyed [the thread that the script created]

and how you destroy threads is usually by using the keyword “return” because it stops the thread

Note/Advice: when you see something said, ALWAYS check if the said thing is true by the means of testing it yourself and checking it
“I was always told”
what you did right here is making an assumption

its very easy to make an assumption and thats one reason why everybody is telling you it

because assumptions are easy to make and a lot of people fall for it

This is being totally over complicated.
Create a local script in StarterPlayerScripts.
Add this code:

local Player = game:GetService("Players").LocalPlayer
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift  and  Player.Character.Humanoid.Health > 0 then
		print("Running")
	end
end)

i am afraid to tell you that this script is actually very simple objectively

No its a unclean way of coding. Just make it simple and easy to understand.

The reason this is in my Character scripts is because it is part of my movement code, which had a lot of required modules depending on the morph you are, or the effect you have on your character. Since those are very ‘character’ related, that location felt appropriate, since when you reset, your morph is back to default and your effects are back to default

i am afraid that you are experiencing this graph

i know what your saying but it is wrong

image

dont worry tho, a lot of coders including me experience this

i’ve already experienced it tho so im somewhere near the left of the middle

(cant really find a good image to show it)

Yeah it is but like i said again, its still firing because the event has not been disconnected. You need to disconnect the event. Im gonna move to a different post because ive tried saying 1000 times.

the solution was in your face the whole time, you just have to put the script inside playerScripts so the input event doesnt get created again after the player dies thus stopping your memory leak

Thanks for the input, I do appreciate it

Thank you for your input as well

I will just have to check for Characer Removing and call maybe a .DeInit() function on the modules and in that function destroy any connections.

They’re correct, when you delete a script any connections created within it are disconnected and the thread’s execution is ceased indefinitely.

while wait() do --do not actually use while wait do in practice
    print"i am here"
end

script:Destroy()

This example wouldn’t work because the while loop runs indefinitely, script:Destroy() is never reached.

task.delay(3, script.Destroy, script)

while task.wait(0.25) do
	print("Hello world!") --Stops printing after 3 seconds.
end
1 Like

yeah adding a coroutine would also fix that.

What script is requiring the ModuleScript? It will itself need to be destroyed upon a player’s CharacterRemoving event/signal being fired.