ModuleScript still running after GUI reset on spawn?

Is this a bug? Every time my player respawn, GUI resets and I require() it with a localscript inside the character. But then when I call a function from the module, the module required previously still runs and prints twice.

Add: Module stays even after destroy?

Could be because requiring the ModuleScript once will cache it.

If you require it from a different place/at a different time, it apparently won’t run its code again and will just give the cached version.

EDIT: Just tested this theory and it’s wrong - they printed different objects.
Screenshot_567
Red line for separation

1 Like

What should I do if I want the module and it’s values to be destroyed? The script I used to require the module is inside the Character and the script itself gets destroyed when the player resets.

Can you show your code?

It shouldn’t be persisting after it has been removed unless you save it to a variable.

Here are all the places where the module is required. 1 is from the localscript inside character and the other 2 is inside the same ScreenGui which ResetOnSpawn.

For how many respawns does the module stay the same? And what does the module do?

Try using require(Module:Clone()) when requiring. It’s a hacky fix, but it should work.

1 Like

The module stores the function when KeyCode.Q is pressed. First time it loads up, the function fires once when Q is pressed, but after resetting a couple times, the function is fire multiple times when Q is pressed. Showing that the previous module in the ScreenGui did not get destroyed.

Add: Even if the module doesn’t store functions, keeping the module cached which isn’t used is just going to take up space which is not ideal for me.

Hmm, I don’t really see the point putting it into the GUI then.
You should keep it with your other script, and at least have full control over everything to do with it’s existence (parent, state, etc.).

I don’t really see the point in fixing this obscure bug when you can easily avoid it by changing how your code works a bit - and the performance impact is minimal.

And also a verbal description isn’t very helpful for debugging :wink:
Code varies greatly from English.

I made a work around, I added a function inside the module to disconnect every listeners when the player dies.

I’m using a module because I have a lot of table connected together for different purposes so it’s convenient to exchange data like stats, my own custom character events, etc…

Thanks every for your help but I’m using the solution by @TheGamer101 from a post I found: ModuleScripts continue to run after having the Destroy() method called on them - #3 by ZhaoDan

1 Like