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?
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
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)
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
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