local T=workspace.FilteringEnabled local z=script.Parent.Car.Value local b=z:WaitForChild("\065\067\054\095\070\069\095\083\111\117\110\100\115")local W=require(z["\065-\067\104\097\115\115\105\115 \084\117\110\101"])local J=0 local K=0 local I=.13 local L=.4 local A=0 local i=0 local d=0 local e=0 local q=0 script:WaitForChild("\082\101\118")script:WaitForChild("\082\101\108")script.Parent.Values.Gear.Changed:connect(function()K=1 if script.Parent.Values.RPM.Value>5000 then q=.2 end end)for T,z in pairs(z.DriveSeat:GetChildren())do for T,b in pairs(script:GetChildren())do if z.Name==b.Name then z:Stop()wait()z:Destroy()end end end b:FireServer("\110\101\119\083\111\117\110\100","\082\101\118",z.DriveSeat,script.Rev.SoundId,0,script.Rev.Volume,true)b:FireServer("\110\101\119\083\111\117\110\100","\082\101\108",z.DriveSeat,script.Rel.SoundId,0,script.Rel.Volume,true)b:FireServer("\112\108\097\121\083\111\117\110\100","\082\101\118")b:FireServer("\112\108\097\121\083\111\117\110\100","\082\101\108")z.DriveSeat:WaitForChild("\082\101\118")z.DriveSeat:WaitForChild("\082\101\108")while wait()do K=math.max(0,K-.1)local s=script.Parent.Values.RPM.Value if script.Parent.Values.Throttle.Value<=W.IdleThrottle/100 then d=math.max(.3,d-.2)A=math.max(0,A-.05)i=1 else d=math.min(1,d+.1)A=1 i=0 end q=math.min(1,q+.2)if script.Parent.Values.RPM.Value>W.Redline-W.RevBounce/4 and script.Parent.Values.Throttle.Value>W.IdleThrottle/100 then e=.5 else e=1 end if script.Parent.Values.PreOn.Value then J=1 local W local s=tick()local U while script.Parent.Values.PreOn.Value do wait()local R=tick()local Y if not U then Y=R-s if Y>=.25 then U=true s=tick()else W=Y*2 end else Y=R-s W=math.max(.5-Y,.12)end RevVolume=(d*q)*e+((((L*i)*A)*(1-d))*math.sin(tick()*50))*J RevPitch=math.max((script.Rev.SetPitch.Value+script.Rev.SetRev.Value*W)*J^2+(I*K)*math.sin(80*tick()),script.Rev.SetPitch.Value)*J RelVolume=((1-RevVolume)*J)/.5 RelPitch=math.max((script.Rel.SetPitch.Value+script.Rel.SetRev.Value*W)*J^2+(I*K)*math.sin(80*tick()),script.Rel.SetPitch.Value)*J if T then b:FireServer("\117\112\100\097\116\101\083\111\117\110\100","\082\101\118",script.Rev.SoundId,RevPitch,RevVolume)b:FireServer("\117\112\100\097\116\101\083\111\117\110\100","\082\101\108",script.Rel.SoundId,RelPitch,RelVolume)else z.DriveSeat.Rev.Volume=RevVolume z.DriveSeat.Rev.Pitch=RevPitch z.DriveSeat.Rel.Volume=RelVolume z.DriveSeat.Rel.Pitch=RelPitch end end else if not script.Parent.IsOn.Value then J=math.max(J-.015,0)else J=1 end RevVolume=(d*q)*e+((((L*i)*A)*(1-d))*math.sin(tick()*50))*J RevPitch=math.max((script.Rev.SetPitch.Value+(script.Rev.SetRev.Value*s)/W.Redline)*J^2+(I*K)*math.sin(80*tick()),script.Rev.SetPitch.Value)*J RelVolume=((1-RevVolume)*J)/.5 RelPitch=math.max((script.Rel.SetPitch.Value+(script.Rel.SetRev.Value*s)/W.Redline)*J^2+(I*K)*math.sin(80*tick()),script.Rel.SetPitch.Value)*J end if T then b:FireServer("\117\112\100\097\116\101\083\111\117\110\100","\082\101\118",script.Rev.SoundId,RevPitch,RevVolume)b:FireServer("\117\112\100\097\116\101\083\111\117\110\100","\082\101\108",script.Rel.SoundId,RelPitch,RelVolume)else z.DriveSeat.Rev.Volume=RevVolume z.DriveSeat.Rev.Pitch=RevPitch z.DriveSeat.Rel.Volume=RelVolume z.DriveSeat.Rel.Pitch=RelPitch end end
--[[
		___      _______                _     
	   / _ |____/ ___/ /  ___ ____ ___ (_)__ 
	  / __ /___/ /__/ _ \/ _ `(_-<(_-</ (_-<
	 /_/ |_|   \___/_//_/\_,_/___/___/_/___/
 						SecondLogic @ Inspare
]]
local FE = workspace.FilteringEnabled
local car = script.Parent.Car.Value
local handler = car:WaitForChild("AC6_FE_Sounds")
local _Tune = require(car["A-Chassis Tune"])
local on = 0
local mult = 0
local det = 0.13
local trm = 0.4
local trmmult = 0
local trmon = 0
local throt = 0
local redline = 0
local shift = 0

script:WaitForChild("Rev")
script:WaitForChild("Rel")

script.Parent.Values.Gear.Changed:connect(function()
	mult = 1
	if script.Parent.Values.RPM.Value > 5000 then
		shift = 0.2
	end
end)

for i, v in pairs(car.DriveSeat:GetChildren()) do
	for _, a in pairs(script:GetChildren()) do
		if v.Name == a.Name then
			v:Stop()
			wait()
			v:Destroy()
		end
	end
end

handler:FireServer("newSound", "Rev", car.DriveSeat, script.Rev.SoundId, 0, script.Rev.Volume, true)
handler:FireServer("newSound", "Rel", car.DriveSeat, script.Rel.SoundId, 0, script.Rel.Volume, true)
handler:FireServer("playSound", "Rev")
handler:FireServer("playSound", "Rel")
car.DriveSeat:WaitForChild("Rev")
car.DriveSeat:WaitForChild("Rel")

while wait() do
	mult = math.max(0, mult - 0.1)
	local _RPM = script.Parent.Values.RPM.Value

	if script.Parent.Values.Throttle.Value <= _Tune.IdleThrottle / 100 then
		throt = math.max(0.3, throt - 0.2)
		trmmult = math.max(0, trmmult - 0.05)
		trmon = 1
	else
		throt = math.min(1, throt + 0.1)
		trmmult = 1
		trmon = 0
	end
	shift = math.min(1, shift + 0.2)
	if
		script.Parent.Values.RPM.Value > _Tune.Redline - _Tune.RevBounce / 4
		and script.Parent.Values.Throttle.Value > _Tune.IdleThrottle / 100
	then
		redline = 0.5
	else
		redline = 1
	end

	if script.Parent.Values.PreOn.Value then
		on = 1
		local _Rev
		local t1 = tick()
		local check
		while script.Parent.Values.PreOn.Value do
			wait()
			local t2 = tick()
			local curr
			if not check then
				curr = t2 - t1
				if curr >= 0.25 then
					check = true
					t1 = tick()
				else
					_Rev = curr * 2
				end
			else
				curr = t2 - t1
				_Rev = math.max(0.5 - curr, 0.12)
			end
			RevVolume = (throt * shift * redline) + (trm * trmon * trmmult * (1 - throt) * math.sin(tick() * 50)) * on
			RevPitch = (
				math.max(
					((script.Rev.SetPitch.Value + script.Rev.SetRev.Value * _Rev) * on ^ 2)
						+ (det * mult * math.sin(80 * tick())),
					script.Rev.SetPitch.Value
				)
			) * on
			RelVolume = (1 - RevVolume) * on / 0.5
			RelPitch = (
				math.max(
					((script.Rel.SetPitch.Value + script.Rel.SetRev.Value * _Rev) * on ^ 2)
						+ (det * mult * math.sin(80 * tick())),
					script.Rel.SetPitch.Value
				)
			) * on
			if FE then
				handler:FireServer("updateSound", "Rev", script.Rev.SoundId, RevPitch, RevVolume)
				handler:FireServer("updateSound", "Rel", script.Rel.SoundId, RelPitch, RelVolume)
			else
				car.DriveSeat.Rev.Volume = RevVolume
				car.DriveSeat.Rev.Pitch = RevPitch
				car.DriveSeat.Rel.Volume = RelVolume
				car.DriveSeat.Rel.Pitch = RelPitch
			end
		end
	else
		if not script.Parent.IsOn.Value then
			on = math.max(on - 0.015, 0)
		else
			on = 1
		end
		RevVolume = (throt * shift * redline) + (trm * trmon * trmmult * (1 - throt) * math.sin(tick() * 50)) * on
		RevPitch = (
			math.max(
				((script.Rev.SetPitch.Value + script.Rev.SetRev.Value * _RPM / _Tune.Redline) * on ^ 2)
					+ (det * mult * math.sin(80 * tick())),
				script.Rev.SetPitch.Value
			)
		) * on
		RelVolume = (1 - RevVolume) * on / 0.5
		RelPitch = (
			math.max(
				((script.Rel.SetPitch.Value + script.Rel.SetRev.Value * _RPM / _Tune.Redline) * on ^ 2)
					+ (det * mult * math.sin(80 * tick())),
				script.Rel.SetPitch.Value
			)
		) * on
	end
	if FE then
		handler:FireServer("updateSound", "Rev", script.Rev.SoundId, RevPitch, RevVolume)
		handler:FireServer("updateSound", "Rel", script.Rel.SoundId, RelPitch, RelVolume)
	else
		car.DriveSeat.Rev.Volume = RevVolume
		car.DriveSeat.Rev.Pitch = RevPitch
		car.DriveSeat.Rel.Volume = RelVolume
		car.DriveSeat.Rel.Pitch = RelPitch
	end
end
