Disconnecting .MouseButton1Click:Connect events questions

ConnectHandler
local ConnectionHandler = {}

local connectionTable = {
	soulSelectionTabs = {},
	soulSelectionSouls = {},
}

function ConnectionHandler.addConnection(tableName, tableInstance)
	table.insert(connectionTable[tableName], tableInstance) -- adds listen event to table
end

function ConnectionHandler.removeConnection(tableName)
	for _, connection in pairs(connectionTable[tableName]) do
		connection:Disconnect() -- disconnect listeners
	end
	table.clear(connectionTable[tableName]) -- clears table
end


return ConnectionHandler
UIHandler
------------------------------ UI INTERACTION FUNCTIONS ------------------------------------------------------

local function setupSoulTreeListeners(slots)
	for name, slot in pairs(slots) do
		local function onSoulIconClick()
			toggleSoulSelectionRemote:FireServer(slot) -- request to open soul selection
			task.wait(0.25) -- wait for server to do its stuff

			for _, soul in pairs(slot.SoulSelection.Scroller:GetChildren()) do
				if soul:IsA("Frame") then
					local function onSoulChooseClick()
						local response = requestSoulChangeRemote:InvokeServer(slot, soul)
						if response == "Success" then
							print(response .. ": You passed!")
							connectionHandler.removeConnection("soulSelectionSouls") -- remove connections after completion
						else
							print(response .. ": Something went wrong!")
							connectionHandler.removeConnection("soulSelectionSouls") -- remove connections after completion
						end
					end

					local soulConnection = soul.Choose.MouseButton1Click:Connect(onSoulChooseClick)
					connectionHandler.addConnection("soulSelectionSouls", soulConnection) -- add connections to each soul in soulSelection
				end
			end
		end

		local iconConnection = slot.SoulIcon.MouseButton1Click:Connect(onSoulIconClick)
		connectionHandler.addConnection("soulSelectionTabs", iconConnection) -- add connections to each soulSelection
	end
end

-- toggle soul tree 
ContextActionService:BindAction("SoulTreeGUI", function(_, UserInputState)
	if UserInputState == Enum.UserInputState.Begin then
		local soulTreeCache = player:FindFirstChild("SoulTreeCache")
		if not soulTreeCache then
			toggleSoulTreeRemote:FireServer("Start")
			setupSoulTreeListeners(slots)
		else
			toggleSoulTreeRemote:FireServer("End")
			connectionHandler.removeConnection("soulSelectionTabs")
		end
	end
end, false, toggleSoulTreeKeyBind)

I am wanting to know if I am disconnecting these events properly or if there is an easier way to do so. This is the only way that I can think of is inserting them into a table and then looping through the table disconnecting each event.

1 Like

Yes storing connections in a table to disconnect later is completely fine to do. There’s not really a “best” proper way to do it unless you care about micro-optimizations that save nanoseconds.

1 Like

Saving the connections in a table is absolutely fine (since there’s really no other basic way).

1 Like