I’m currently programming a generic item system that will be used in a few projects that I’m working on. One thing I’m currently stuck on is how to correctly handle the memory management of weapons under this item system.
For example, if a player were to have a sword, a new script would need to be executed for that player’s particular sword (much like you would see if you were using the Tool instance). Said script would obviously be handling the sword’s input, state, and animations. However, if the sword is destroyed (due to player respawn, or it was dropped and cleaned up by DebrisService), the script handling it would also need to be destroyed.
What happens when a script/localscript/modulescript is destroyed or disabled? Is the thread that corresponds with that luasourcecontainer terminated? Is the memory used by that thread automatically cleaned/garbage collected? If so, are connections in that memory automatically disconnected? If not, is the thread in memory forever, since there’s a reference that wasn’t garbage collected?
I’m curious as to how this works, so I can properly manage the memory usage of seperate weapon/tool/item instances that have scripts running them.
When using script.disabled it terminates the thread, but can be brought back up and restart again as said in this article. As for destroying a script, it will no longer be available to be used upon and will be locked from the server’s use as said from this article.
Destroying a script will definitely help improve memory since it disposes the unnecessary thing, disabling the script is just there so you can reuse it, not sure exactly why it wants to finish off the script before actually disabling though.
Edit: I think I found the reason to your script.disable problem, you can’t actually use it inside the script itself as said in the article I provided above.
“Note, the above code snippet cannot be used within the script itself. This is because once the script is disabled the thread will terminate.”
Is there a reason as to why that occurs. I am curious myself. I remember hearing a script is re-enabled if the script connected to an event and that event is fired. However I haven’t found any source. Should probably test myself but you already made the thread sooooo…