local R=script.Parent.Car.Value local k=script.Parent.Values local t=require(R["\065-\067\104\097\115\115\105\115 \084\117\110\101"])local u=R:WaitForChild("\083\111\117\110\100\115")local n={}local L={}local W=k.RPM local P=k.Throttle local y=t.Redline local b=t.RevBounce+100 local C=1-ThrottleVolMult local c=1-ThrottlePitchMult local function p(R,k)for t=0,4,1 do local u=Instance.new("\083\111\117\110\100")if t==1 then u.Name="\069\120\104\097\117\115\116"u.Parent=R.Body.Exhaust u.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..k[1]u.Volume=0 u.RollOffMaxDistance=k[2]u.RollOffMinDistance=k[3]local t=Instance.new("\069\113\117\097\108\105\122\101\114\083\111\117\110\100\069\102\102\101\099\116",u)t.Name="\069\049"t.HighGain=k[13][1]t.MidGain=k[13][2]t.LowGain=k[13][3]local n=Instance.new("\068\105\115\116\111\114\116\105\111\110\083\111\117\110\100\069\102\102\101\099\116",u)n.Name="\069\050"local L=Instance.new("\069\113\117\097\108\105\122\101\114\083\111\117\110\100\069\102\102\101\099\116",u)L.Name="\069\051"L.MidGain=0 elseif t==2 then u.Name="\069\110\103\105\110\101"u.Parent=R.Body.Engine u.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..k[4]u.Volume=0 u.RollOffMaxDistance=k[5]u.RollOffMinDistance=k[6]local t=Instance.new("\069\113\117\097\108\105\122\101\114\083\111\117\110\100\069\102\102\101\099\116",u)t.Name="\069\049"t.HighGain=k[14][1]t.MidGain=k[14][2]t.LowGain=k[14][3]local n=Instance.new("\068\105\115\116\111\114\116\105\111\110\083\111\117\110\100\069\102\102\101\099\116",u)n.Name="\069\050"local L=Instance.new("\069\113\117\097\108\105\122\101\114\083\111\117\110\100\069\102\102\101\099\116",u)L.Name="\069\051"L.MidGain=0 elseif t==3 then u.Name="\073\100\108\101"u.Parent=R.Body.Exhaust u.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..k[7]u.Volume=0 u.RollOffMaxDistance=k[8]u.RollOffMinDistance=k[9]elseif t==4 then u.Name="\082\101\100\108\105\110\101"u.Parent=R.Body.Engine u.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..k[10]u.Volume=0 u.RollOffMaxDistance=k[11]u.RollOffMinDistance=k[12]end u.Looped=true table.insert(n,t,u)end end local function a(R,k)if n[1].Volume~=k[1]then n[1].Volume=k[1]n[1].Pitch=k[2]n[2].Volume=k[3]n[2].Pitch=k[4]n[3].Volume=k[5]n[4].Volume=k[6]n[1].E2.Level=k[7]n[1].E3.HighGain=k[9][1]n[1].E3.MidGain=k[9][2]n[1].E3.LowGain=k[9][3]n[2].E2.Level=k[8]n[2].E3.HighGain=k[10][1]n[2].E3.MidGain=k[10][2]n[2].E3.LowGain=k[10][3]end end local function S()for R,k in pairs(n)do k:Stop()end end local function A()for R,k in pairs(n)do k:Play()end end local function w(k)local W=P.Value local p=k/y L[1]=((k^ExhaustVolMult/y)*(W*ThrottleVolMult+C))*ExhaustVol L[2]=math.max(ExhaustPitch+(ExhaustRev*p)*(W*ThrottlePitchMult+c),ExhaustPitch)L[3]=((k^EngineVolMult/y)*(W*ThrottleVolMult+C))*EngineVol L[4]=math.max(EnginePitch+(EngineRev*p)*(W*ThrottlePitchMult+c),EnginePitch)L[5]=IdleVol+k/(-t.IdleRPM-IdleRPMScale)L[6]=(((((k+b)-y)/y)*W)*RedlineVol)*100 L[7]=W*(ExhaustDistortion+ExhaustRevDistortion*p)L[8]=W*(EngineDistortion+EngineRevDistortion*p)L[9]={ExhaustDecelEqualizer[1]*(1-W),ExhaustDecelEqualizer[2]*(1-W);ExhaustDecelEqualizer[3]*(1-W)}L[10]={EngineDecelEqualizer[1]*(1-W),EngineDecelEqualizer[2]*(1-W),EngineDecelEqualizer[3]*(1-W)}if FE==true then u:FireServer(R,"\117\112\100\097\116\101",nil,L)else a(n,L)end end createinfo={ExhaustId;ExhaustRollOffMax;ExhaustRollOffMin;EngineId,EngineRollOffMax,EngineRollOffMin,IdleId;IdleRollOffMax;IdleRollOffMin,RedlineId,RedlineRollOffMax;RedlineRollOffMin,ExhaustEqualizer;EngineEqualizer}if FE==true then u:FireServer(R,"\099\114\101\097\116\101",createinfo)else p(R,createinfo)end if FE==true then if k.Parent.IsOn.Value==true then u:FireServer(R,"\112\108\097\121")else u:FireServer(R,"\115\116\111\112")end else if k.Parent.IsOn.Value==true then A()else S()end end k.Parent.IsOn.Changed:Connect(function(k)if FE==true then if k==false then u:FireServer(R,"\115\116\111\112")else u:FireServer(R,"\112\108\097\121")end else if k==true then A()else S()end end end)W.Changed:Connect(w)


local car = script.Parent.Car.Value
local values = script.Parent.Values
local tune = require(car["A-Chassis Tune"])
local event = car:WaitForChild('Sounds')
local newsounds={}
local info={}

local rpm=values.RPM
local throt=values.Throttle
local redl=tune.Redline
local bounce=tune.RevBounce+100
local vmult=1-ThrottleVolMult
local pmult=1-ThrottlePitchMult

local function create(car,t)
	for k=0,4 do
		local ns=Instance.new('Sound')
		if k==1 then
			ns.Name='Exhaust'
			ns.Parent=car.Body.Exhaust
			ns.SoundId='rbxassetid://'..t[1]
			ns.Volume=0
			ns.RollOffMaxDistance=t[2]
			ns.RollOffMinDistance=t[3]
			local e1=Instance.new('EqualizerSoundEffect',ns)
			e1.Name='E1'
			e1.HighGain=t[13][1]
			e1.MidGain=t[13][2]
			e1.LowGain=t[13][3]
			local e2=Instance.new('DistortionSoundEffect',ns)
			e2.Name='E2'
			local e3=Instance.new('EqualizerSoundEffect',ns)
			e3.Name='E3'
			e3.MidGain=0
		elseif k==2 then
			ns.Name='Engine'
			ns.Parent=car.Body.Engine
			ns.SoundId='rbxassetid://'..t[4]
			ns.Volume=0
			ns.RollOffMaxDistance=t[5]
			ns.RollOffMinDistance=t[6]
			local e1=Instance.new('EqualizerSoundEffect',ns)
			e1.Name='E1'
			e1.HighGain=t[14][1]
			e1.MidGain=t[14][2]
			e1.LowGain=t[14][3]
			local e2=Instance.new('DistortionSoundEffect',ns)
			e2.Name='E2'
			local e3=Instance.new('EqualizerSoundEffect',ns)
			e3.Name='E3'
			e3.MidGain=0
		elseif k==3 then
			ns.Name='Idle'
			ns.Parent=car.Body.Exhaust
			ns.SoundId='rbxassetid://'..t[7]
			ns.Volume=0
			ns.RollOffMaxDistance=t[8]
			ns.RollOffMinDistance=t[9]
		elseif k==4 then
			ns.Name='Redline'
			ns.Parent=car.Body.Engine
			ns.SoundId='rbxassetid://'..t[10]
			ns.Volume=0
			ns.RollOffMaxDistance=t[11]
			ns.RollOffMinDistance=t[12]
		end
		ns.Looped=true
		table.insert(newsounds,k,ns)
	end
end

local function update(sounds,info)
	if newsounds[1].Volume~=info[1] then 
		newsounds[1].Volume=info[1]
		newsounds[1].Pitch=info[2]
		newsounds[2].Volume=info[3]
		newsounds[2].Pitch=info[4]
		newsounds[3].Volume=info[5] 
		newsounds[4].Volume=info[6]
		newsounds[1].E2.Level=info[7]
		newsounds[1].E3.HighGain=info[9][1]
		newsounds[1].E3.MidGain=info[9][2]
		newsounds[1].E3.LowGain=info[9][3]
		newsounds[2].E2.Level=info[8]
		newsounds[2].E3.HighGain=info[10][1]
		newsounds[2].E3.MidGain=info[10][2]
		newsounds[2].E3.LowGain=info[10][3]
	end
end

local function stop()
	for _,s in pairs(newsounds) do s:Stop() end
end

local function play()
	for _,s in pairs(newsounds) do s:Play() end
end

local function rpmupdate(x)
	local throtv=throt.Value
	local rpmscale=(x/redl)
	info[1]=((x^ExhaustVolMult)/redl)*((throtv*ThrottleVolMult)+vmult)*ExhaustVol								-- Exhaust Volume
	info[2]=math.max(ExhaustPitch + ExhaustRev *rpmscale*((throtv*ThrottlePitchMult)+pmult),ExhaustPitch)		-- Exhaust Pitch
	info[3]=((x^EngineVolMult)/redl)*((throtv*ThrottleVolMult)+vmult)*EngineVol									-- Engine Volume
	info[4]=math.max(EnginePitch + EngineRev *rpmscale*((throtv*ThrottlePitchMult)+pmult),EnginePitch)			-- Engine Pitch
	info[5]=IdleVol+(x/(-tune.IdleRPM-IdleRPMScale))															-- Idle Volume
	info[6]=(((x+bounce)-redl)/redl)*throtv*RedlineVol*100														-- Redline Volume
	info[7]=throtv*(ExhaustDistortion+(ExhaustRevDistortion*rpmscale))											-- Exhaust Distortion
	info[8]=throtv*(EngineDistortion+(EngineRevDistortion*rpmscale))											-- Engine Distortion
	info[9]={ExhaustDecelEqualizer[1]*(1-throtv),ExhaustDecelEqualizer[2]*(1-throtv),ExhaustDecelEqualizer[3]*(1-throtv)}	-- Exhaust Decel Equalizer
	info[10]={EngineDecelEqualizer[1]*(1-throtv),EngineDecelEqualizer[2]*(1-throtv),EngineDecelEqualizer[3]*(1-throtv)}		-- Engine Decel Equalizer
	if FE==true then
		event:FireServer(car,'update',nil,info)
	else
		update(newsounds,info)
	end
end

createinfo = {
	ExhaustId,
	ExhaustRollOffMax,
	ExhaustRollOffMin,
	EngineId,
	EngineRollOffMax,
	EngineRollOffMin,
	IdleId,
	IdleRollOffMax,
	IdleRollOffMin,
	RedlineId,
	RedlineRollOffMax,
	RedlineRollOffMin,
	ExhaustEqualizer,
	EngineEqualizer,
}

if FE==true then
	event:FireServer(car,'create',createinfo)
else
	create(car,createinfo)
end

if FE==true then
	if values.Parent.IsOn.Value==true then
		event:FireServer(car,'play')
		else event:FireServer(car,'stop')
	end
else
	if values.Parent.IsOn.Value==true then
		play() else stop()
	end
end

values.Parent.IsOn.Changed:Connect(function(v)
	if FE==true then
		if v==false then event:FireServer(car,'stop')
		else event:FireServer(car,'play') end
	else
		if v==true then
			play() else stop()
		end
	end
end)

rpm.Changed:Connect(rpmupdate)
