Functions/threads created by calling ModuleScript functions through destroyed Script are also destroyed

Assuming you have a Script or LocalScript that calls a function from a ModuleScript and destroys itself immediately,

local f:()->() = require(game.ReplicatedStorage.SomeModuleScript)
f()

script:Destroy()

and a ModuleScript that has a function that creates a thread that waits,

return function()
	task.spawn(function()
		task.wait(1)
		print("Hello, World!")
	end)
end

the waiting thread in the ModuleScript will not run after the delay. In addition, any function or thread created from calling a ModuleScript’s functions will not run after the calling script is destroyed.

Below is a place that replicates this issue.
script-modulescript-destruction.rbxl (55.9 KB)

Expected behavior

The expected behavior from the example is that "Hello, World!" will be printed after 1 second of delay, even after the calling Script/LocalScript is destroyed.

In general, the expected behavior is that a thread or function created from calling a function from a ModuleScript will not be destroyed/disabled after the calling Script/LocalScript is destroyed.

This isn’t a bug and it has been this way since forever.
When a script is destroyed / disabled, all its threads are cancelled, including threads created using functions from other scripts

3 Likes

this is intentional, creating a new require is like creating a new table;

once the script gets deleted, the LUAU garbage collector will collect everything that was with the script (unless its a global item or instance to my knowledge)

required modulescripts/metatables aren’t instances

1 Like

I’m marking this won’t fix because as far as I am aware this is the intended behavior, and changing it now would not be backwards compatible.

2 Likes