How would i disable Connections?

Before you start saying "oh do Connection:Disconnect()",
I already know.

I’m more of wondering how would I Disconnect an Array of Objects that where applied the connection via for loop:

for _,i in pairs(Example:GetChildren()) do
   if i:isA("BasePart") then
      i.Touched:Connect(function(part) -- how would i disconnect all these Events?
         -- lets say there is code here
      end
   end
end)

How would I go about in doing this?
Thanks

2 Likes

Store your connections inside a dictionary whenever you’re connecting them.

local CurrentConnections = {} :: {[string]: RBXScriptConnection}
for _,i in pairs(Example:GetChildren()) do
   if i:isA("BasePart") then
      CurrentConnections[i.Name] = i.Touched:Connect(function(part) 
         -- lets say there is code here
      end)
   end
end)

Then you can disconnect any connection via its index which is the instance’s name or through iterating the table via a loop.

If you want to disconnect an array of objects after that, you can also iterate through the array and check if the given instance name does exist in the dictionary.

for _, Inst in ipairs(Example:GetChildren()) do
	if CurrentConnections[Inst.Name] then
		CurrentConnections[Inst.Name]:Disconnect()
		CurrentConnections[Inst.Name] = nil
	end
end
4 Likes

I was about to Delete the post,

Also this does not work, it prints cannot read value as a string, nor does it disonnect the functions

More information?!

Maybe you can try to create a local variable containing the connected function and then insert it into the table.

@EgizianoEG’s code should work, just define the table without anymore information:

local CurrentConnections = {}

And also consider saving it using the instance itself as a key instead of it’s name so no conflicts may happen.

1 Like

Yeah, this is the right way to do it how did I forget this.

2 Likes

that’s what I did, nothing happens

there are no errors. no disconnections, nothing is happening besides the for loop already connecting the Events

I’m afraid the problem is something else then, I just tested the code myself and it seemed to have worked just fine.

Here’s the exact code that I used:

local connections = {}

for _, part in pairs(script.Parent:GetChildren()) do
	if part:IsA("BasePart") then
		connections[part] = part.Touched:Connect(function()
			print("touch")
		end)
	end
end

task.wait(20)

for _, connection in pairs(connections) do
	connection:Disconnect()
end

print("disconnected")

Are you sure you are disconnecting the connections properly or maybe something else?

1 Like

Yes

local CurrentConnections = {}
for _,i in pairs(script.Parent:GetChildren()) do
	if i:isA("BasePart") then
		CurrentConnections[i.Name] = i.Touched:Connect(function(part) 
			print("E")
		end)
	end
end
print("Check 1")
task.wait(10)

for _, Inst in ipairs(CurrentConnections) do
		CurrentConnections[Inst]:Disconnect() -- doesnt disconnect
		CurrentConnections[Inst] = nil
end
print("Check 2") -- doesnt print

Thats literally all i did, and the disconnections dont fire

It seems that doing ipairs does not work, use pairs and save the connections using the instance instead of the instance’s name.

still, didn’t change anything :frowning:

Isn’t ‘Inst’ in the code the rbxscriptsignal? Since the index is the parts name and the value the rbxscriptsignal?
If im getting this right, then shouldn’t it be

for _, connection in pairs(CurrentConnections) do
		connection:Disconnect() 
		connection = nil
end

?

P.S: When the index is not a number ipairs does not run.

Still does the exact same thing, nothing

Can you send your current code? I’m genuinely confused.

This is my Current code :confused:

Just remove the [Inst] part

On the disconnect loop, use pairs() instead of ipairs(), using ipairs() doesn’t even make the code run:

for part, connection in pairs(CurrentConnections) do
	print(part, connection)
	connection:Disconnect()
	CurrentConnections[part] = nil
end

Also very important, save using the instance instead of the name!

Tried this, it prints Check 2 (With pairs and ipairs), still doesnt disconnect

Output:

 22:17:35.584  Check 1  -  Server - Script:9 -- First Check
  22:17:35.716   ▶ E (x78)  -  Server - Script:5 -- Initial Touch
  22:17:45.595  Check 2  -  Server - Script:16 -- Second Check
  22:17:45.611   ▶ E (x384)  -  Server - Script:5 -- Character Touching Parts
-- doesnt disconnect

Are you saving using the instance?

You should do this:

CurrentConnections[i] = i.Touched:Connect(function(part) 
	print("E")
end)

Instead of:

CurrentConnections[i.Name] = i.Touched:Connect(function(part) 
	print("E")
end)

So if parts have the same name they don’t overwrite each other.

2 Likes

Ah, thats what I was doing wrong, i forgot to change it on the first loop.

It works now, thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.