Better code structurer?

Is there any good way to make this into 1 if statement?

	if CircleUIConnector ~= nil then
		CircleUIConnector:Disconnect()
		CircleUIConnector = nil
	end
	if CircleArrowUIConnector ~= nil then
		CircleArrowUIConnector:Disconnect()
		CircleArrowUIConnector = nil
	end

Not really, you could make a cleanup module for this? Something similar to maid. Basically you would add the connections to a table and then when you want to disconnect you can run :Disconnect() or :Clean() etc.

local Tasks = {}

local function Give(task)
   table.insert(Tasks, task)
end

local function Disconnect()
   for _, task in Tasks do
      task:Disconnect()
      task = nil
   end
end

--> Example
local Part = workspace.Part
Give(Part.Touched:Connect(function(hit)
   print("Part was touched")
end))

Disconnect() -- ^^ will no longer run and has been removed

This is the very basic way of writing it, of course you give as many tasks as you want. In your case it would be something like this:

Give(CircleUIConnector) --> You can also just write the Connect line in here
Give(CircleArrowUIConnector)

--> When you want to disconnect
Disconnect()
1 Like

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