Unable to disconnect a connection, causing stacking

Hello. This is basically a poor attempt to create a skill slot system. You got 8 slots, each corresponding to keys like z,x,c,v and then you got the skills. First you choose the key, and then you choose the skill to assign it to the said key.

We got a main GUI that has 8 different buttons. When you click on any of them, the script detects the name of the button and finds its corresponding bool value inside the player, meaning if you clicked on Z button the Z bool inside the player is assigned to a variable. This is done with the for i, v in pairs loop. Then another GUI pops up. This new GUI has undefined amounts of buttons, it entirely depends on the player’s data. So when it is opened, the script checks for the current buttons with the for s, l in pairs loop. When a button is pressed, the name of the button is assigned to the bool’s value that I mentioned before.

local conn0
local conn1
local conn2
conn0 = loadoutButton.MouseButton1Click:Connect(function()
	print("1")
	skillLoadoutFrame.Visible = true
	loadoutButton.Visible = true
	
	for i, v in pairs(skillLoadoutFrame:GetChildren()) do
		
		if v.Name == "closeButton" then return end
		
		conn1 = v.MouseButton1Click:Connect(function()
			local playerKeys = player:WaitForChild("Loadout")
			local key = playerKeys:WaitForChild(v.Name)
			
			skillsListCloseButton.Visible = true
			knownSkillsOuterFrame.Visible = true
						
			for s, l in pairs(knownSkillsFrame:GetChildren()) do
			
				if l.Name ~= "UIListLayout" then
					
					conn2 = l.MouseButton1Click:Connect(function()
						print("Conn2 Connected")
						conn1:Disconnect()
						conn2:Disconnect()
						if key.Value == l.Name then return end --Zaten bu slota bu skill takılı
						for kf, k in pairs(playerKeys:GetChildren()) do
							if k.Value == l.Name then
								k.Value = ""
							end
						end
						key.Value = l.Name
						knownSkillsOuterFrame.Visible = false
					end)

				end
			end
		end)
		print("conn1 Connected")

	end
end)

loadoutCloseButton.MouseButton1Click:Connect(function()
	print("1")
	skillLoadoutFrame.Visible = false
	if conn1 then
		conn1:Disconnect()
		print("disconnected")
	end
	if conn2 then
		conn2:Disconnect()
	end
	if conn0 then
		--conn0:Disconnect()
	end
end)

skillsListCloseButton.MouseButton1Click:Connect(function()
	knownSkillsOuterFrame.Visible = false
	if conn1 then
		conn1:Disconnect()
	end
	if conn2 then
		conn2:Disconnect()
	end
end)

Now the issue is that the code is stacking with every click. When I close the GUI and open it again it starts to print the same line more than once. The code is broken beyond that, like the key value and some other stuff does not update when you click to a new button but I believe that the origin of that error is the same as stacking. So I believe I can fix everything once I understand how to use :Disconnect properly. So yeah, can anyone please explain why my :Disconnect lines are not doing anything? I want Conn1 to disconnect when the GUI is closed via “loadoutCloseButton”, but it just won’t work. It keeps stacking and stacking.

3 Likes

just superficially checking the code

for i, v in pairs(skillLoadoutFrame:GetChildren()) do
  if v.Name == "closeButton" then return end
    conn1 = v.MouseButton1Click:Connect(function()
    ...

here, conn1 is reassigned to a new connection for each non-close button. if we have 8 skill buttons, then all 8 will have the click connected, and then only the last button’s connection is stored into conn1

1 Like
local connections = {}

for i, v in pairs(skillLoadoutFrame:GetChildren()) do
    if v.Name == "closeButton" then return end
        table.insert(connections, v.MouseButton1Click:Connect(function()

        end))
    end
end

--> disconnect
for i, v in pairs(connections) do
    v:Disconnect()
end
table.clear(connections)
2 Likes

Ah I see. Thank you for explaining

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