--Airflow = script.Parent.Parent.ChopperNB


ChopperNB = script.Parent.Parent.ChopperNB
MuteNB = ChopperNB.Mute
LoadWeight = script.LoadWeight
MVal = script.MuteVal
RotorMotor = script.Parent.Parent.Parent.Motor.Motor.HingeConstraint
LoadWeight.Value = LoadWeight.NoBlowerW.Value

script.Parent.BlowerOn.Changed:Connect(function()
	if script.Parent.BlowerOn.Value == false then
		--Airflow.HighAir.Disabled = true
		--Airflow.LowAir.Disabled = false
		local ts = game:GetService("TweenService")
		local t = ts:Create(script.Parent,TweenInfo.new(5),{Volume = 0})
		local t3 = ts:Create(MVal,TweenInfo.new(4.4),{Value = 0})
		local t2 = ts:Create(script.Parent.SoundComponents.NonAdjustable.BlowerOffsetPitch,TweenInfo.new(1),{Value = 0.1})
		local t4 = ts:Create(LoadWeight,TweenInfo.new(3),{Value = LoadWeight.NoBlowerW.Value})
		--print("o1")
		t:Play()
		t2:Play()
		t3:Play()
		t4:Play()
	else
		--Airflow.HighAir.Disabled = false
		--Airflow.LowAir.Disabled = true
		local ts = game:GetService("TweenService")
		local t = ts:Create(script.Parent,TweenInfo.new(.85),{Volume = 4.5})
		local t3 = ts:Create(MVal,TweenInfo.new(5),{Value = -80})
		local t2 = ts:Create(script.Parent.SoundComponents.NonAdjustable.BlowerOffsetPitch,TweenInfo.new(2),{Value = 0})
		local t4 = ts:Create(LoadWeight,TweenInfo.new(.85),{Value = LoadWeight.BlowerW.Value})
		--print("o2")
		t:Play()
		t2:Play()
		t3:Play()
		t4:Play()
	end
end)

function getChopperLevelPitch(pitch)
	return 0
	--math.clamp((pitch-0.1)/script.Parent,0,1)
end

script.Parent.PlaybackSpeed = 0

CompletelyStopped = true

script.Parent.On.Changed:Connect(function()
	if CompletelyStopped == true and script.Parent.On.Value == true then
		repeat game:GetService("RunService").Heartbeat:wait()

			CompletelyStopped = false
			script.Stopped.Value = false
			
			RotorMotor.AngularVelocity = script.Parent.PlaybackSpeed/0.00335
			
			ChopperNB.PlaybackSpeed = script.Parent.PlaybackSpeed --/0.6801
			ChopperNB.Volume = script.Parent.PlaybackSpeed/.9
			MuteNB.HighGain = MVal.Value
			MuteNB.LowGain = MuteNB.HighGain
			MuteNB.MidGain = MuteNB.HighGain

			script.Parent.RollOffMaxDistance = script.RollOffMax.Value *script.Parent.Volume --7000
			script.Parent.RollOffMinDistance = script.RollOffMin.Value *script.Parent.Volume --7000

			if script.Parent.On.Value == true and script.Parent.PlaybackSpeed < script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+BlowerAddress.BeltSet.Sound.Pitch/7 then
				if script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value < 1 then
					if script.Parent.SoundComponents.NonAdjustable.NormalSound.Value < math.abs(script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value-1) then
						script.Parent.SoundComponents.NonAdjustable.NormalSound.Value = math.abs(script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value-1)
					end
				end
				local clampval = math.clamp(math.ceil(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value),0,1)
				local pitch = script.Parent.SoundComponents.NonAdjustable.NormalSound.Value
				local formula = math.clamp(math.log(pitch+0.001,0.65)/320,0.00007,0.02)--pitch+0.001,0.75)/320,0.00007,0.02
				--print(script.Parent.PlaybackSpeed)
				--print(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value)
				script.Parent.SoundComponents.NonAdjustable.NormalSound.Value = math.clamp(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value + formula,0,2)
				script.Parent.PlaybackSpeed = (script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value+((BlowerAddress.BeltSet.Sound.Pitch/7)*clampval)))-1
			elseif script.Parent.On.Value == false and script.Parent.PlaybackSpeed > 0 then
				if script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value < 1 then
					if script.Parent.SoundComponents.NonAdjustable.NormalSound.Value < math.abs(script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value-1) then
						script.Parent.SoundComponents.NonAdjustable.NormalSound.Value = 0
					end
				end
				local pitch = script.Parent.SoundComponents.NonAdjustable.NormalSound.Value
				--local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.06)/300,0.0004,0.07)
				local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.05)/LoadWeight.Value,0.0004,0.02)
				local clampval = math.clamp(math.ceil(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value),0,1)
				--print(formula)
				script.Parent.SoundComponents.NonAdjustable.NormalSound.Value = math.clamp(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value - formula,0,2)
				--+(script.Parent.Shift.Octave*math.clamp(0,1,math.ceil(math.abs(formula))))
				script.Parent.PlaybackSpeed = ((script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value+((BlowerAddress.BeltSet.Sound.Pitch/7)*clampval))-1)*clampval)
			else
				local clampval = math.clamp(math.ceil(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value),0,1)
				script.Parent.PlaybackSpeed = ((script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+(script.Parent.SoundComponents.NonAdjustable.NormalSound.Value+((BlowerAddress.BeltSet.Sound.Pitch/7)*clampval))-1)*clampval)
			end
	--[[	if script.Parent.On.Value == true then
		
		--((math.clamp((incerment+0.001)-script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value/2,0,1) * 30))
		if script.Parent.PlaybackSpeed >= script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value then

		else
			--math.abs((((script.Parent.SoundComponents.NonAdjustable.ChopperLevelCurrent.Value-4)/20)+1
			formula = (script.Parent.SoundComponents.NonAdjustable.StaticVoltage.Value*(1))/(70000*math.sqrt((script.Parent.SoundComponents.NonAdjustable.LoadWeight.Value*((script.Parent.PlaybackSpeed+0.0001)/(script.Parent.SoundComponents.Configuration.ChopperLevel.Value-(script.Parent.PlaybackSpeed*(5-(math.abs(script.Parent.SoundComponents.Configuration.ChopperLevel.Value-4.6)))))))))
			--formula = (((script.Parent.SoundComponents.NonAdjustable.StaticVoltage.Value+script.Parent.SoundComponents.NonAdjustable.ChopperLevelCurrent.Value)/(incerment+0.01))*script.Parent.SoundComponents.NonAdjustable.Horsepower.Value)/(80000*math.sqrt((script.Parent.SoundComponents.NonAdjustable.LoadWeight.Value)*(script.Parent.SoundComponents.NonAdjustable.Horsepower.Value)))
		end
	else
		if script.Parent.PlaybackSpeed <= 0 then
			
		else
			formula = (incerment+0.001)/(((500/(incerment+0.001))+math.sqrt(script.Parent.SoundComponents.NonAdjustable.LoadWeight.Value)))
		end
	end
	--print(formula)
	--+math.sqrt(script.Parent.SoundComponents.NonAdjustable.LoadWeight.Value)
	--print((incerment+0.001)/(((500/(incerment+0.001))+math.sqrt(script.Parent.SoundComponents.NonAdjustable.LoadWeight.Value))))
	if script.Parent.On.Value == true then
		if script.Parent.PlaybackSpeed >= script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+script.Parent.SoundComponents.NonAdjustable.BlowerOffsetPitch.Value then
			if script.Parent.PlaybackSpeed >= script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value then
				if script.Parent.PlaybackSpeed <= script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value then

				else
					script.Parent.PlaybackSpeed =  math.clamp(script.Parent.PlaybackSpeed - math.clamp(formula,0.000005,0.001),0,script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+script.Parent.SoundComponents.NonAdjustable.BlowerOffsetPitch.Value)
				end
			end
		else
			script.Parent.PlaybackSpeed = math.clamp(script.Parent.PlaybackSpeed + formula,0,script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+script.Parent.SoundComponents.NonAdjustable.BlowerOffsetPitch.Value)
		end
	else
		if script.Parent.PlaybackSpeed <= 0 then
			
		else
			script.Parent.PlaybackSpeed =  math.clamp(script.Parent.PlaybackSpeed - math.clamp(formula,0.000005,0.001),0,script.Parent.SoundComponents.NonAdjustable.MaxPitch.Value+script.Parent.SoundComponents.NonAdjustable.BlowerOffsetPitch.Value)
		end
	end
	--]]


		until script.Parent.SoundComponents.NonAdjustable.NormalSound.Value <= 0
		CompletelyStopped = true
		RotorMotor.AngularVelocity = 0
		script.Parent.PlaybackSpeed = 0
		ChopperNB.PlaybackSpeed = 0
		
		if BlowerAddress.Components.BlowerOn.Value == false then
			script.Parent.Volume = 0
		end
		script.Stopped.Value = true
	end
end)

script.Parent.BlowerOn.Changed:Connect(function()
	if script.Parent.BlowerOn.Value == false and CompletelyStopped == true then
		script.Parent.Volume = 0
	end
end)