local x=script.Parent.Car.Value local g=script.Parent.Values local u=require(x["\065-\067\104\097\115\115\105\115 \084\117\110\101"])local e=x:WaitForChild("\066\111\111\115\116\083\111\117\110\100\115")local Y={}local v=g.Boost local R=g.Throttle local G=-1000 local function E(x,g)for u=0,3,1 do local e=Instance.new("\083\111\117\110\100")e.Parent=x.Body.Engine if u==1 then e.Name="\084\117\114\098\111"e.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..g[1]e.Volume=0 e.RollOffMaxDistance=g[2]e.RollOffMinDistance=g[3]e.Looped=true elseif u==2 then e.Name="\083\117\112\101\114"e.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..g[4]e.Volume=0 e.RollOffMaxDistance=g[5]e.RollOffMinDistance=g[6]e.Looped=true elseif u==3 then e.Name="\066\111\118"e.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..g[7]e.Volume=0 e.RollOffMaxDistance=g[8]e.RollOffMinDistance=g[9]e.Looped=false end e:Play()table.insert(Y,u,e)end end local function r(x,g)if Y[1].Volume~=g[1]then Y[1].Volume=g[1]Y[1].Pitch=g[2]Y[2].Volume=g[3]Y[2].Pitch=g[4]Y[3].Volume=g[5]Y[3].Pitch=g[6]end end local function j()for x,g in pairs(Y)do g:Play()end end local function V()for x,g in pairs(Y)do g:Stop()end end local function W(x)info[1]=TurboVol*math.floor(g.BoostTurbo.Value)info[2]=TurboSetPitch+TurboPitchIncrease*g.BoostTurbo.Value info[5]=BovVol*math.floor(g.BoostTurbo.Value)info[6]=BovSetPitch+BovPitchIncrease*g.BoostTurbo.Value end local function U(x)info[3]=SuperVol*g.BoostSuper.Value info[4]=SuperSetPitch+SuperPitchIncrease*g.BoostSuper.Value end local function l(x)info[1]=TurboVol*math.floor(g.BoostTurbo.Value)info[2]=TurboSetPitch+TurboPitchIncrease*g.BoostTurbo.Value info[3]=SuperVol*g.BoostSuper.Value info[4]=SuperSetPitch+SuperPitchIncrease*g.BoostSuper.Value info[5]=BovVol*math.floor(g.BoostTurbo.Value)info[6]=BovSetPitch+BovPitchIncrease*g.BoostTurbo.Value end info={TurboId;TurboRollOffMax;TurboRollOffMin,SuperId;SuperRollOffMax;SuperRollOffMin;BovId,BovRollOffMax;BovRollOffMin}if FE==true then e:FireServer(x,"\099\114\101\097\116\101",info)if g.Parent.IsOn.Value==true then e:FireServer(x,"\112\108\097\121")else e:FireServer(x,"\115\116\111\112")end elseif FE==false then E(x,info)if g.Parent.IsOn.Value==true then j()else V()end end if BovId>0 then R.Changed:Connect(function()local g=v.Value wait(.1)if v.Value+BovSensitivity<g then if FE==true then e:FireServer(x,"\098\111\118\112\108\097\121")else Y[3]:Play()end end end)end if u.Turbochargers>0 and u.Superchargers==0 then info[3]=0 info[4]=0 v.Changed:Connect(function(g)W()if FE==true then e:FireServer(x,"\117\112\100\097\116\101",nil,info)else r(Y,info)end end)elseif u.Superchargers>0 and u.Turbochargers==0 then info[1]=0 info[2]=0 info[5]=0 info[6]=0 v.Changed:Connect(function(g)U(g)if FE==true then e:FireServer(x,"\117\112\100\097\116\101",nil,info)else r(Y,info)end end)elseif u.Turbochargers>0 and u.Superchargers>0 then v.Changed:Connect(function(g)l(g)if FE==true then e:FireServer(x,"\117\112\100\097\116\101",nil,info)else r(Y,info)end end)end g.Parent.IsOn.Changed:Connect(function(g)if g==false and FE==true then e:FireServer(x,"\115\116\111\112")elseif g==true and FE==true then e:FireServer(x,"\112\108\097\121")elseif g==false and FE==false then V()elseif g==true and FE==false then j()end end)


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

local bst=values.Boost
local throt=values.Throttle
local pbst=-1000

local function create(car,info)
	for k=0,3 do
		local ns=Instance.new('Sound')
		ns.Parent=car.Body.Engine
		if k==1 then
			ns.Name='Turbo'
			ns.SoundId='rbxassetid://'..info[1]
			ns.Volume=0
			ns.RollOffMaxDistance=info[2]
			ns.RollOffMinDistance=info[3]
			ns.Looped=true
		elseif k==2 then
			ns.Name='Super'
			ns.SoundId='rbxassetid://'..info[4]
			ns.Volume=0
			ns.RollOffMaxDistance=info[5]
			ns.RollOffMinDistance=info[6]
			ns.Looped=true
		elseif k==3 then
			ns.Name='Bov'
			ns.SoundId='rbxassetid://'..info[7]
			ns.Volume=0
			ns.RollOffMaxDistance=info[8]
			ns.RollOffMinDistance=info[9]
			ns.Looped=false
		end
		ns:Play()
		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[3].Pitch=info[6]
	end
end
local function play()
	for _,s in pairs(newsounds) do s:Play() end
end
local function stop()
	for _,s in pairs(newsounds) do s:Stop() end
end
local function turboupd(x)
	info[1]=TurboVol*math.floor(values.BoostTurbo.Value)
	info[2]=TurboSetPitch+(TurboPitchIncrease*values.BoostTurbo.Value)
	info[5]=BovVol*math.floor(values.BoostTurbo.Value)
	info[6]=BovSetPitch+(BovPitchIncrease*values.BoostTurbo.Value)
end
local function superupd(x)
	info[3]=SuperVol*values.BoostSuper.Value
	info[4]=SuperSetPitch+(SuperPitchIncrease*values.BoostSuper.Value)
end
local function twinupd(x)
	info[1]=TurboVol*math.floor(values.BoostTurbo.Value)
	info[2]=TurboSetPitch+(TurboPitchIncrease*values.BoostTurbo.Value)
	info[3]=SuperVol*values.BoostSuper.Value
	info[4]=SuperSetPitch+(SuperPitchIncrease*values.BoostSuper.Value)
	info[5]=BovVol*math.floor(values.BoostTurbo.Value)
	info[6]=BovSetPitch+(BovPitchIncrease*values.BoostTurbo.Value)
end

info = {TurboId,TurboRollOffMax,TurboRollOffMin,SuperId,SuperRollOffMax,SuperRollOffMin,BovId,BovRollOffMax,BovRollOffMin}
if FE==true then
	event:FireServer(car,'create',info)
	
	if values.Parent.IsOn.Value==true then
		event:FireServer(car,'play')
	else event:FireServer(car,'stop')
	end
elseif FE==false then
	create(car,info)
	
	if values.Parent.IsOn.Value==true then
		play()
	else
		stop()
	end
end

if BovId>0 then
	throt.Changed:Connect(function()
		local pbst=bst.Value
		wait(.1)
		if bst.Value+BovSensitivity<pbst then
			if FE==true then
				event:FireServer(car,'bovplay')
			else
				newsounds[3]:Play()
			end
		end
	end)
end

if tune.Turbochargers>0 and tune.Superchargers==0 then
	info[3]=0
	info[4]=0
	bst.Changed:Connect(function(x)
		turboupd()
		if FE==true then
			event:FireServer(car,'update',nil,info)
		else
			update(newsounds,info)
		end
	end)
	
elseif tune.Superchargers>0 and tune.Turbochargers==0 then
	info[1]=0
	info[2]=0
	info[5]=0
	info[6]=0
	bst.Changed:Connect(function(x)
		superupd(x)
		if FE==true then
			event:FireServer(car,'update',nil,info)
		else
			update(newsounds,info)
		end
	end)
	
elseif tune.Turbochargers>0 and tune.Superchargers>0 then
	bst.Changed:Connect(function(x)
		twinupd(x)
		if FE==true then
			event:FireServer(car,'update',nil,info)
		else
			update(newsounds,info)
		end
	end)
end
	
values.Parent.IsOn.Changed:Connect(function(v)
	if v==false and FE==true then event:FireServer(car,'stop')
	elseif v==true and FE==true then event:FireServer(car,'play')
	elseif v==false and FE==false then stop()
	elseif v==true and FE==false then play() end
end)
