local dataLink = script.DataLink.Value

local frequency = dataLink.singleSendFrequency
local bits = {}

function toBits(num)
	-- returns a table of bits, least significant first.
	local t = {} -- will contain the bits
	while num > 0 do
		local rest = math.fmod(num, 2)
		t[#t + 1] = rest
		num = (num - rest) / 2
	end
	while table.getn(t) < 7 do
		table.insert(t, 0)
	end
	return t
end

for _, part in pairs(script.Parent:GetChildren()) do
	if part.Name == string.match(part.Name, '^Bit.*$') then
		bits[tonumber(string.match(part.Name, '%d'))] = part
	end
end


function connect()
	if dataLink.isDataSingleSendActive.Value then
		for i, bit in pairs(toBits(frequency.Value)) do
			if bit == 0 then
				bits[i].Bulb.Material = Enum.Material.Plastic
			end
			if bit == 1 then
				bits[i].Bulb.Material = Enum.Material.Neon
			end
		end
	end
end

frequency.Changed:Connect(connect)
