local dataLink = script:WaitForChild("Data Link").Value

local mouseDown = false
local center = script.Parent:GetModelCFrame()


function rotateMod(part, rotation)
	part.CFrame = (center * rotation * (center:inverse() * part.CFrame))
end

local knob = script.Parent.Knob

function goForward()
	while mouseDown do
		if dataLink.singleSendFrequency.Value >= 127 then
			knob.Break.Playing = true
			rotateMod(knob, CFrame.Angles(0, -0.3, 0))
			wait(.4)
			rotateMod(knob, CFrame.Angles(0, 0.3, 0))
			dataLink.singleSendFrequency.Value = 127
			Leave()
			return
		end
		dataLink.singleSendFrequency.Value = dataLink.singleSendFrequency.Value + 1
		local step = 1
		for i = 0, 5, step do
			rotateMod(knob, CFrame.Angles(0, -0.2, 0))
			wait()
		end
	end
end

function goBack()
	while mouseDown do
		if dataLink.singleSendFrequency.Value <= 0 then
			knob.Break.Playing = true
			rotateMod(knob, CFrame.Angles(0, 0.3, 0))
			wait(.4)
			rotateMod(knob, CFrame.Angles(0, -0.3, 0))
			dataLink.singleSendFrequency.Value = 0
			Leave()
			return
		end
		dataLink.singleSendFrequency.Value = dataLink.singleSendFrequency.Value - 1
		local step = 1
		for i = 0, 5, step do
			rotateMod(knob, CFrame.Angles(0, 0.2, 0))
			wait()
		end
	end
end


function leftClick()
	if mouseDown then
		return
	end
	mouseDown = true
	knob.Sound.PlaybackSpeed = 1
	knob.Sound.Volume = 0.5
	goForward()
end

function rightClick()
	if mouseDown then
		return
	end
	mouseDown = true
	knob.Sound.PlaybackSpeed = 0.8
	knob.Sound.Volume = 0.6
	goBack()
end


function Leave()
	mouseDown = false
	knob.Sound.Volume = 0
end


script.Parent.Use.MouseClick:connect(leftClick)
script.Parent.Use.RightMouseClick:Connect(rightClick)
script.Parent.Use.MouseHoverLeave:connect(Leave)

