How to keep a function in a module running even after script that called it is destroyed

Short example

local mod = require(game.ReplicatedStorage.ModuleScript)

mod:Start()

task.wait(1)

script:Destroy()
local module = {}

function module:Start()
task.spawn(function()
	while true do
		print(1)
		
		task.wait()
	end
end)
end

return module

The prints in the while loop stop printing once the localscript has been destroyed. How can I keep the while loop going? I need this all stored in a function in the module, and I NEED the localscript to be destroyed, no compromises.

1 Like

Would cloning the script and destroying the old one be feasible? Roblox recently changed the behavior of scripts such that infinitely looping code is terminated upon the script’s deletion.

Why don’t you fire a bindable event instead? It will still run even though the script is destroyed.

Your problem is probably there because of a design issue. What’s your use case?

put script.Disabled=true on top of the script

like this:

script.Disabled=true

local mod = require(game.ReplicatedStorage.ModuleScript)

mod:Start()

task.wait(1)

script:Destroy()