I’ve got a complicated GUI script and it has a bug. I think my bug stems from the fact that :Disconnect() doesn’t close all of the connections made with a singular variable.
--Put this script in an ImageButton
local Connection
local function Test()
Connection = script.Parent.Activated:Connect(function()
print("Button Pressed")
end)
end
Test()
Test()
Connection:Disconnect()
Each time you press the button, it will print “Button Pressed” instead of printing nothing. If you comment out the last line of this script, it will print “Button Pressed” twice for every time you press the button. Is there any way to :Disconnect() ALL of the connections?
function makeConnection()
table.insert(connections, script.Parent.Activated:Connect(function()
print("Test!")
end))
-- Basically inserts a new connection.
end
makeConnection()
makeConnection()
-- Inserts two new connections in the 'connections' table.
wait(5) -- waits 5 or more seconds.
for i, v in pairs(connections) do
v:Disconnect() -- disconnect connection.
end
This will make a table for all your connections, and when makeConnection() is called, it inserts a new connection in the table. This will last for 5 seconds, then every connection will be disconnected.
local ConnectionGroup = {}
ConnectionGroup.__index = ConnectionGroup
function ConnectionGroup.Create(ExisitingConnections)
local Proxy = {}
Proxy.Connections = {}
if ExisitingConnections then
for i = 1,#ExisitingConnections do
Proxy.Connections[i] = ExisitingConnections[i]
end
end
setmetatable(Proxy, ConnectionGroup)
return Proxy
end
function ConnectionGroup:AddConnection(Connection)
for i = 1,#self.Connections do
if self.Connections[i] == Connection then
warn "CANNOT ADD DUPLICATE CONNECTION CAUSE WHATS THE POINT OF THAT LOL"
return
end
end
table.insert(self.Connections,Connection)
end
function ConnectionGroup:RemoveConnection(Connection)
for i = 1,#self.Connections do
if self.Connections[i] == Connection then
self.Connections[i]:Disconnect()
table.remove(self.Connections, i)
return
end
end
end
function ConnectionGroup:Disconnect()
for i = 1,#self.Connections do
self.Connections[i]:Disconnect()
table.remove(self.Connections, i)
end
end
local Proxy = ConnectionGroup.Create() --- well lets add the connections later cause it kinda neater i gues?
Proxy:AddConnection(workspace.Baseplate.Touched:Connect(function()
print "hello"
end))
local Connection = workspace.ChildAdded:Connect(function()
print "cool"
end)
Proxy:AddConnection(Connection)
Proxy:AddConnection(Connection) -- its gonna warn no need for a duplicate connection
Proxy:RemoveConnection(Connection)
wait(10)
Proxy:Disconnect()
print "PPROXY DISCONNECTED"
Allows you to just do :Disconnect() and disconnects all added connections.
More advanced version if u wanna have cooler features just wrote it:
local ConnectionGroup = {}
ConnectionGroup.Groups = {}
ConnectionGroup.__index = ConnectionGroup
function ConnectionGroup.Create(Name, ExisitingConnections)
local Proxy = {}
Proxy.Connections = {}
if Name == nil then
warn "pls put a name"
return
end
if ConnectionGroup.Groups[Name] then
warn "put a unique name.........."
return
end
Proxy.Name = Name
ConnectionGroup.Groups[Name] = Proxy
if ExisitingConnections then
for i = 1,#ExisitingConnections do
Proxy.Connections[i] = ExisitingConnections[i]
end
end
setmetatable(Proxy, ConnectionGroup)
return Proxy
end
function ConnectionGroup:AddConnection(Connection)
for i = 1,#self.Connections do
if self.Connections[i] == Connection then
warn "CANNOT ADD DUPLICATE CONNECTION CAUSE WHATS THE POINT OF THAT LOL"
return
end
end
table.insert(self.Connections,Connection)
end
function ConnectionGroup:RemoveConnection(Connection)
for i = 1,#self.Connections do
if self.Connections[i] == Connection then
self.Connections[i]:Disconnect()
table.remove(self.Connections, i)
return
end
end
end
function ConnectionGroup:Disconnect()
for i = 1,#self.Connections do
self.Connections[i]:Disconnect()
table.remove(self.Connections, i)
end
end
function ConnectionGroup:Destroy()
ConnectionGroup.Groups[self.Name] = nil
self:Disconnect()
end
function ConnectionGroup.GetGroupByName(name)
return ConnectionGroup.Groups[name]
end
local Proxy = ConnectionGroup.Create("WorkspaceStuff") --- well lets add the connections later cause it kinda neater i gues?
Proxy:AddConnection(workspace.Baseplate.Touched:Connect(function()
print "hello"
end))
local Connection = workspace.ChildAdded:Connect(function()
print "cool"
end)
Proxy:AddConnection(Connection)
Proxy:AddConnection(Connection) -- its gonna warn no need for a duplicate connection
Proxy:RemoveConnection(Connection)
wait(10)
Proxy:Disconnect()
print "PPROXY DISCONNECTED" --- we didnt reallyy it so it would free out of memory, we just disconnected all the connections lets free it out of memory at the end of the script
----------------------------
ConnectionGroup.Create("BasePlateStuff")
local Proxy2 = ConnectionGroup.GetGroupByName('BasePlateStuff')
Proxy2:AddConnection(workspace.Baseplate.Touched:Connect(function()
print "hello"
end))
wait(5)
--- DESTORY BOTH CONNECTION GROUPS
Proxy2:Destroy()
ConnectionGroup.GetGroupByName('WorkspaceStuff'):Destroy() -- we need to desconsturct it using my destory method so we can free it out of memory
Btw here I wrote an advanced version just right now if u wanna get more more advanced