I couldn’t be confident on a specific category where this type of post would fit. Notify me if it should go somewhere else.
The Dilemma
Some of you may have run into an issue or question in your trials and tribulations with ModuleScripts where a piece of your code still executes, despite the Script that required it being missing.
ModuleScripts do in-fact keep running after death, death specifically being through means of :Destroy()
Script
require(workspace.ModuleScript)
ModuleScript
local module = {}
while task.wait(0.5) do
print("e")
end
return module
Example1.rbxl (54.6 KB)
Running this code, will print “e” into the Output log as expected, and deleting both the Script AND the ModuleScript, still prints “e”, from a nil source.
If this behaviour benefits you, enjoy the rest of your day!
But if you were looking for an answer on how to avoid this from happening, I have an answer for this as well.
The Solution
All you need is to turn your code snippet into a module function!
Script
require(workspace.ModuleScript).setup()
ModuleScript
local module = {}
function module.setup()
while task.wait(0.5) do
print("e")
end
end
return module
Example2.rbxl (54.6 KB)
Upon running it, simply deleting the Server Script is enough to cease the entire process.
I hope this article of knowledge has made someone’s day a little bit easier!