I have a Touched event connected to a simple function and I want to stop detecting it.
But it's a specific situation where I got to disconnect it from inside the function and (if I use the Disconnect() method) the variable is local, something like this:
local connection = part.Touched:connect(function()
if condition then
connection:Disconnect()
end
end)
But it doesn't seem to work. Like this, it outputs the "attempt to index a nil value" error.
Is there something I could do to solve this? The event has to stop, because it will be assigned to a large number of parts, so I want to prevent unnecessary running code. And the connection has to be local because everything is inside a GetChildren() loop.
yikes

2 comments
Do you just mean ending the event/function? Because you could just do a return
Return would stop the function, right? I want to prevent it from running, the Touched can't happen.