How to disconnect functions inside a loop

There isn’t much to say, I just want to know if it is possible to disconnect functions that are called like this:

for i=1, 100 do
   local part ="Part",workspace)
   local touch = part.Touched:Connect(function()
       touch:Disconnect() --I want the function to disconnect once it is fired
       print("doesn't work")


  • If I make “touch” a global variable it doesn’t seem to be disconnecting the function, and if I keep it as a local one, it prints an error saying that I tried to disconnect a nil value.

(I know it’s not a lot, I just don’t have any other ideas)

I would like to know what’s the easiest or most efficient way to do this, any help is greatly appreciated, and if it just can’t be done, it would still be nice to know.

NOTE: I do know that destroying the parts will disconnect the functions automatically, but I don’t want this to be an option, I only want the parts to live without calling the function more than once.

1 Like

Do this:

local touch

touch = part.Touched:Connect(function()

sorry but, where is the loop?

I know that functions can be disconnected like that, but we’re talking about functions called inside loops here

Sorry, wdym? If you put that code inside of the loop it’ll do what you were originally trying to do.

oh, I see, sorry

I didn’t have it very clear at first

1 Like