How would I be able to have these link to each other?

I have a wiring system in a building game I’m making, but I’m having trouble figuring out how to make the delay blocks link back to each other.

I’m trying to achieve something like this:

This is what happens when I try to do the same in my game:

In the server script, it detects if the object is already linked and unlinks it by checking if there is an object value called either “inputObj” or “outputObj”. When I try linking the second delay back to the first delay, it detects one of the object values and unlinks it instead.


Wire tool client script:

local Mouse = game.Players.LocalPlayer:GetMouse()
local Positiona
local target
local sbh = script.Parent.SelectionBox_Hover
local equippedval

local selectedObjInput = {}
local selectedObjOutput = {}

local debounce = false

local state = true

local player = game.Players.LocalPlayer
local char = player.Character

script.Parent.Activated:Connect(function()
	if target then
		if target.Parent.Parent == workspace.Blocks then
			if target.Parent:FindFirstChild("Wireable") then
				if target.Parent:FindFirstChild("Selected") then
					--
				else
					if target.Parent.Wireable.Value == "Input" then
						table.insert(selectedObjInput, target.Parent)
						local sb = script.Parent.SelectionBox_Selected:Clone()
						sb.Parent = target.Parent
						sb.Adornee = target.Parent.Root
						sb.Name = "Selected"
					elseif target.Parent.Wireable.Value == "Output" then
						table.insert(selectedObjOutput, target.Parent)
						local sb = script.Parent.SelectionBox_Selected:Clone()
						sb.Parent = target.Parent
						sb.Adornee = target.Parent.Root
						sb.Name = "Selected"
					end
				end
				if #selectedObjInput > 0 and #selectedObjOutput > 0 then
					if debounce == false then
						debounce = true
						if state == true then
							script.Parent.link:FireServer(selectedObjInput, selectedObjOutput, true)
						end
					end
				end
			else
				if target:FindFirstChild("Selected") then
					--
				else
					if target.Name == "InputPart" then
						table.insert(selectedObjInput, target.Parent)
						local sb = script.Parent.SelectionBox_Selected:Clone()
						sb.Parent = target
						sb.Adornee = target
						sb.Name = "Selected"
					elseif target.Name == "OutputPart" then
						table.insert(selectedObjOutput, target.Parent)
						local sb = script.Parent.SelectionBox_Selected:Clone()
						sb.Parent = target
						sb.Adornee = target
						sb.Name = "Selected"
					end
					if #selectedObjInput > 0 and #selectedObjOutput > 0 then
						if debounce == false then
							debounce = true
							if state == true then
								script.Parent.link:FireServer(selectedObjInput, selectedObjOutput, true)
							end
						end
					end
				end
			end
		end
	end
end)

Mouse.Move:Connect(function()
	target = Mouse.Target
	if equippedval == true and target ~= nil then
		if target.Parent.Parent == workspace.Blocks and target.Parent:FindFirstChild("Wireable") or target.Name == "InputPart" or target.Name == "OutputPart" then
			Mouse.TargetFilter = target.Parent.Root
			if target.Name == "InputPart" or target.Name == "OutputPart" then
				sbh.Adornee = target
			else
				sbh.Adornee = target.Parent
			end
		else
			sbh.Adornee = nil
		end
	end
end)

script.Parent.Equipped:Connect(function()
	equippedval = true
	for _, v in pairs(workspace.Blocks:GetChildren()) do
		if v.owner.Value == player.UserId and v:FindFirstChild("Wireable") then
			local box = script.Parent.SelectionBox_Clickable:Clone()
			box.Parent = v
			box.Adornee = v
			for _, v in pairs(workspace.Blocks:GetChildren()) do
				if v.owner.Value == player.UserId then
					for _, obj in pairs(v:GetChildren()) do
						if obj:IsA("Beam") then
							obj.Enabled = true
						end
					end
				end
			end
		elseif v:FindFirstChild("InputPart") and v:FindFirstChild("OutputPart") then
			local box1 = script.Parent.SelectionBox_Clickable:Clone()
			box1.Parent = v
			box1.Adornee = v.InputPart
			local box2 = script.Parent.SelectionBox_Clickable:Clone()
			box2.Parent = v
			box2.Adornee = v.OutputPart
			for _, v in pairs(workspace.Blocks:GetChildren()) do
				if v.owner.Value == player.UserId then
					for _, obj in pairs(v:GetChildren()) do
						if obj:IsA("Beam") then
							obj.Enabled = true
						end
					end
				end
			end
		end
	end
end)

script.Parent.Unequipped:Connect(function()
	equippedval = false
	sbh.Adornee = nil
	table.clear(selectedObjInput)
	table.clear(selectedObjOutput)
	for _, v in pairs(workspace.Blocks:GetChildren()) do
		if v.owner.Value == player.UserId then
			for _, obj in pairs(v:GetDescendants()) do
				if obj:IsA("SelectionBox") then
					obj:Destroy()
				end
				if obj:IsA("Beam") then
					obj.Enabled = false
				end
			end
		end
	end
	for _, v in pairs(player.PlayerGui:GetChildren()) do
		if v.Name == "BindingList" then
			v:Destroy()
		end
	end
end)

script.Parent.link.OnClientEvent:Connect(function(unlink)
	for _, v in pairs(workspace.Blocks:GetChildren()) do
		if v.owner.Value == player.UserId then
			for _, obj in pairs(v:GetDescendants()) do
				if obj.Name == "Selected" then
					obj:Destroy()
				end
			end
		end
	end
	local attach0
	local attach1
	local beam
	if unlink == false then
		for _, v in pairs(selectedObjOutput) do
			beam = game.ReplicatedStorage.Bind:Clone()
			beam.Parent = v
			local inputObj = Instance.new("ObjectValue", beam)
			inputObj.Name = "input"
			attach1 = Instance.new("Attachment", v.Root)
			attach1.Name = "Bind1"
			attach1.Parent = v.Root
			attach1.WorldCFrame = v.Root.CFrame
			beam.Attachment1 = attach1
			for _, v in pairs(selectedObjInput) do
				attach0 = Instance.new("Attachment", v.Root)
				attach0.Name = "Bind0"
				attach0.Parent = v.Root
				attach0.WorldCFrame = v.Root.CFrame
				inputObj.Value = v
				if beam then
					beam.Attachment0 = attach0
				end
			end
		end
	else
		for _, v in pairs(selectedObjOutput) do
			for _, b in pairs(v:GetChildren()) do
				if b:IsA("Beam") then
					for _, c in pairs(selectedObjInput) do
						if b.input.Value == c then
							b:Destroy()
						end
					end
				end
			end
		end
	end
	table.clear(selectedObjInput)
	table.clear(selectedObjOutput)
	debounce = false
end)

player.CharacterAdded:Connect(function()
	table.clear(selectedObjInput)
	table.clear(selectedObjOutput)
	for _, v in pairs(workspace.Blocks:GetChildren()) do
		if v.owner.Value == player.UserId then
			for _, obj in pairs(v:GetDescendants()) do
				if obj:IsA("SelectionBox") then
					obj:Destroy()
				end
				if obj:IsA("Beam") then
					obj.Enabled = false
				end
			end
		end
	end
end)

Wire tool server script:

script.Parent.link.OnServerEvent:Connect(function(player, obj1, obj2, link)
	local unlink = false
	if link == true then
		for _, obj in pairs(obj1) do
			for _, Obj in pairs(obj:GetChildren()) do
				if Obj:IsA("ObjectValue") then
					for _, v in pairs(obj2) do
						if Obj.Value == v then
							Obj:Destroy()
							unlink = true
						end
					end
				end
			end 
		end

		for _, obj in pairs(obj2) do
			for _, Obj in pairs(obj:GetChildren()) do
				if Obj:IsA("ObjectValue") then
					for _, v in pairs(obj1) do
						if Obj.Value == v then
							Obj:Destroy()
							unlink = true
						end
					end
				end
			end 
		end
		
		if unlink == false then
			for i, v in pairs(obj1) do
				for i, b in pairs(obj2) do
					local outputObj = Instance.new("ObjectValue", v)
					outputObj.Name = "OutputObj"
					outputObj.Value = b
				end
			end

			for i, v in pairs(obj2) do
				for i, b in pairs(obj1) do
					local inputObj = Instance.new("ObjectValue", v)
					inputObj.Name = "inputObj"
					inputObj.Value = b
				end
			end
		end
	end
	script.Parent.link:FireClient(player, unlink)
end)