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)
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
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?
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
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.
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