Cancelling a module script?

So, I have a module script that runs different functions when fired (when a player clicks a button).

I want to make it to where when the Player dies, the module script stops firing whatever is currently running, no matter where the script has currently progressed to.

So far I’ve tried deleting the module script, but that did nothing.

3 Likes

Have you tried disconnecting event connections?

Imagine we’re connecting a Touched event to a BasePart.

local connection; connection = BasePart.Touched:Connect(function(p)
   if p.Parent:IsA"Model" then
      if p.Parent:FindFirstChildOfClass"Humanoid" then
         print"WOO!! We found a humanoid"
         connection:Disconnect()
      end
   end
end)

delay(10, function()
   if connection then
      print"Welp. No humanoid found within 10 seconds. Disconnecting the finder event.."
      connection:Disconnect()
   end
end)
1 Like

Unfortunately, there is no proper way to stop a coroutine/function.

Be careful because you can “stop” them, it just requires conditional statements. But I wouldn’t really say stop. Functions just hold code that can be run when called. If the function has a loop, have it check for a condition that you set when you want to “stop” it and then break the loop and return the function (if there is code after that you don’t want to run). As with coroutines, the same method applies. You can also yield coroutines which effectively pauses them until resumed.

1 Like

Like @Trizxistan has said you could assign the Connected functions to a variable or if there are more, maybe in a table or similar. Then on Humanoid.Died event, you could Disconnect the event.

Actually it would be better if you show what kind of Function your script is firing? So we could assist you quicker, hopefully.

1 Like

Totally agree. But ongoing loops inside a coroutine would be impossible to stop if it doesn’t include break or return.

I went with if statements, and it worked. But I feel like there’s an easier way I could’ve done this. I’ll mark you as solution unless someone presents something easier.

For anyone who didn’t use a bunch of for loops, this method wouldn’t have worked. Fortunately my script was filled with for loops to put the statements in.