local dataLink = script["Data Link"].Value
local button = script.Parent.Parent.SingleSendReversePolarity
local bulb = script.Parent.Parent.ActiveLED.Bulb
local bits = {}
local running = false

for _, part in pairs(script.Parent.Parent:GetChildren()) do
	if part.Name == string.match(part.Name, '^Bit.*$') then
		table.insert(bits, part)
	end
end


script.Parent.Use.MouseClick:Connect(function()
	if dataLink.isDataSingleSendActive.Value then
		running = true
		if dataLink.isDataSingleSendReverseActive.Value == false then
			button.Position = Vector3.new(button.Position.X + 0.02, button.Position.Y - 0.065, button.Position.Z - 0.011)
			dataLink.isDataSingleSendReverseActive.Value = true
			dataLink.singleSendFrequency.Value = 0
			for _, part in pairs(bits) do
				part.Bulb.Material = Enum.Material.Plastic
			end
			bulb.Color = Color3.fromRGB(245, 205, 48)

		elseif dataLink.isDataSingleSendReverseActive.Value then
			button.Position = Vector3.new(button.Position.X - 0.02, button.Position.Y + 0.065, button.Position.Z + 0.011)
			dataLink.isDataSingleSendReverseActive.Value = false
			dataLink.isDataSingleListenAutoTravelActive.Value = false
			for _, part in pairs(bits) do
				part.Bulb.Material = Enum.Material.Plastic
			end
			bulb.Color = Color3.fromRGB(0, 255, 0)
		end
		running = false
	end
end)


dataLink.isDataSingleSendReverseActive.Changed:Connect(function()
	if not running then
		if dataLink.isDataSingleSendReverseActive.Value then
			button.Position = Vector3.new(button.Position.X + 0.02, button.Position.Y - 0.065, button.Position.Z - 0.011)
			bulb.Color = Color3.fromRGB(245, 205, 48)

		elseif dataLink.isDataSingleSendReverseActive.Value == false then
			button.Position = Vector3.new(button.Position.X - 0.02, button.Position.Y + 0.065, button.Position.Z + 0.011)
			bulb.Color = Color3.fromRGB(0, 255, 0)
		end
	end
end)
