Make code running from module continue even after script that required it is destroyed

As the title says, I want to do something like this and make it so when this code is called, it continues as it should

1 Like

Could you give an example of what you mean?

put this on top of your script

script.Disabled=true
task.delay(0,function()
        script.Parent=nil
end)

do not destroy your script

2 Likes

But it continues. Modules don’t stop running even if you delete the script that required them.

This try this, it will demonstrate what i am saying:

-- Module named ModuleScript placed at ReplicatedStorage
local TestAmount = 200;

--- Prints (TestAmount *2) every second.
-- If it did in fact stop running after the script who called it got destroyed, this printing should stop.
while true do
	TestAmount*= 2
	print(`Running\nTest {test}`)
	task.wait(1)
end

return nil 
-- Nil must be returned to prevent requiring errors, even though the module does 
-- not achieve this point on execution because of the while-do loop.
-- Server script placed in ServerScriptService.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TestModule = require(ReplicatedStorage.ModuleScript)
--- Server script placed below the script above

-- Delays for 5 seconds and then deletes the script that required the module
task.delay(5, function()
	script.Parent:Destroy()
	print(`Destroyed the script`)
end)

((
This test in format of rbxm file:
TestingModules.rbxm (1.9 KB)
))

Example structure:

image

And this is the output:
image

You can see that it keeps going, even after the caller script gets destroyed.

1 Like

try to use :Remove() instead of :Destroy().

in my game there are moves that activate by using a tool, which it then requires a module for the attack. however, if a player respawns after hitting someone with a move that applies a debuff, the debuff would never end…

1 Like