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

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)
