How would I disconnect these two event inside of a i,v pairs loop

I tried to disconnecting the events inside of the i,v pairs loop, but it didn’t work correctly. How would you disconnect is correctly?

local HoverAudio = game.ReplicatedStorage.Audio:FindFirstChild("HoverAudio")
local ClickAudio = game.ReplicatedStorage.Audio:FindFirstChild("ClickAudio")
local BoolValue = game.ReplicatedStorage.BoolValue
local currentConnection = nil


BoolValue:GetPropertyChangedSignal('Value'):Connect(function()
	if currentConnection ~= nil then 
		currentConnection:Disconnect()
	end
		
	if BoolValue.Value == true then
		for i,v in pairs(script.Parent.Home.HomeImage.ImageFolder:GetDescendants()) do
			if v:IsA("ImageButton") then
				currentConnection = v.MouseEnter:Connect(function()--- I want to disconnect this
					HoverAudio:Play()
				end)
				currentConnection = v.MouseButton1Click:Connect(function()--- I want to disconnect this
					ClickAudio:Play()
				end)
			end
		end
	end
end)
	



You’re making two connections, so when you set currentConnection to a connection, the old one will be “lost”, or unreachable. Here it is improved:

local HoverAudio = game.ReplicatedStorage.Audio:FindFirstChild("HoverAudio")
local ClickAudio = game.ReplicatedStorage.Audio:FindFirstChild("ClickAudio")
local BoolValue = game.ReplicatedStorage.BoolValue
local currentConnection1 = nil
local currentConnection2 = nil


BoolValue:GetPropertyChangedSignal('Value'):Connect(function()
    if currentConnection1 ~= nil then 
		currentConnection1:Disconnect()
	end
    if currentConnection2 ~= nil then 
		currentConnection2:Disconnect()
	end	
	if BoolValue.Value == true then
		for i,v in pairs(script.Parent.Home.HomeImage.ImageFolder:GetDescendants()) do
			if v:IsA("ImageButton") then
				currentConnection1 = v.MouseEnter:Connect(function()--- I want to disconnect this
					HoverAudio:Play()
				end)
				currentConnection2 = v.MouseButton1Click:Connect(function()--- I want to disconnect this
					ClickAudio:Play()
				end)
			end
		end
	end
end)
	



1 Like

You need to create a different variable to assign the MouseButton1Click connection instead of using a single connection.

local HoverAudio = game.ReplicatedStorage.Audio:FindFirstChild("HoverAudio")
local ClickAudio = game.ReplicatedStorage.Audio:FindFirstChild("ClickAudio")
local BoolValue = game.ReplicatedStorage.BoolValue
local currentConnection = nil
local currentConnection2 = nil


BoolValue:GetPropertyChangedSignal('Value'):Connect(function()
	if currentConnection ~= nil then 
		currentConnection:Disconnect()
	end
    if currentConnection2 ~= nil then
        currentConnection2:Disconnect()
    end
		
	if BoolValue.Value == true then
		for i,v in pairs(script.Parent.Home.HomeImage.ImageFolder:GetDescendants()) do
			if v:IsA("ImageButton") then
				currentConnection = v.MouseEnter:Connect(function()--- I want to disconnect this
					HoverAudio:Play()
				end)
				currentConnection2 = v.MouseButton1Click:Connect(function()--- I want to disconnect this
					ClickAudio:Play()
				end)
			end
		end
	end
end)```
1 Like

You cannot disconnect it after you connect it, because OP checks if the value of the instance is true which would connect it if not but disconnect it just incase before so it doesn’t disconnect right after connection.

Sorry, I thought they were trying to disconnect it after the loop. I fixed the post now

You can store the connections in a table

local connections = {}

--to add a connection
connections.onenter = v.MouseEnter:Connect(....)
--or
table.insert(connections, v.MouseEnter:Connect(....))
--to clear the connections
for i, v in next, connections do
     v:Disconnect()
end
2 Likes

I see no reason to disconnect the events if all you’re gonna do is reconnect them once a player hovers over the object or click it.
I would understand it it’s never gonna be used again in the server.

Regardless, it makes no difference, but I wouldn’t set it up just to set it up, and then you’ll have two useless if statements and variables.

Simply leave their connection alone if you’re gonna use it later.


Anyways, here’s how you can use :Disconnect() properly.
Read carefully, there’s a lot of information that will be useful on this topic.

1 Like