local P=script.Parent.Car.Value local x=script.Parent.Values local g=require(P["\065-\067\104\097\115\115\105\115 \084\117\110\101"])local U=P:WaitForChild("\069\078\071\073\078\069\095\080\065\067\075")local E=workspace.FilteringEnabled local s=nil for P,x in pairs(P.Misc:GetDescendants())do if x:IsA("\079\098\106\101\099\116\086\097\108\117\101")and x.Name=="\069\078\071\073\078\069\095\080\065\067\075"then s=x.Value end end local F=s:FindFirstChild("\067\097\109\095\071\101\097\114\115")local y=s:FindFirstChild("\069\110\103\105\110\101\095\083\104\097\107\101")local w=s:FindFirstChild("\077\097\110\105\102\111\108\100\095\072\101\097\116")local u=s:FindFirstChild("\084\104\114\111\116\116\108\101\095\066\111\100\121")local p=s:FindFirstChild("\084\105\109\105\110\103\095\066\101\108\116")local f=s:FindFirstChild("\084\117\114\098\111\099\104\097\114\103\101\114\115")local r=0 local m=x.Throttle.Changed:Connect(function()if E then U:FireServer("\084\104\114\111\116\116\108\101",x.Throttle.Value,u,THROTTLE_BODY)else if THROTTLE_BODY and u~=nil then for P,g in pairs(u:GetChildren())do if string.sub(g.Name,1,4)=="\066\111\100\121"then local P=g.W:FindFirstChildOfClass("\077\111\116\111\114")P.DesiredAngle=x.Throttle.Value*1.5 end end end end end)local J=x.RPM.Changed:Connect(function()if E then U:FireServer("\082\080\077",x.RPM.Value,F,y,p,CAM_GEARS,ENGINE_SHAKE,TIMING_BELT,GEAR_SPEED,SHAKE_VALUE,BELT_SPEED)else if CAM_GEARS and F~=nil then for P,g in pairs(F:GetChildren())do if string.sub(g.Name,1,4)=="\071\101\097\114"then local P=g.W:FindFirstChildOfClass("\077\111\116\111\114")P.DesiredAngle=9000000000.0 P.MaxVelocity=(x.RPM.Value/1000)*GEAR_SPEED end end end if ENGINE_SHAKE and y~=nil then local P=y.W:FindFirstChildOfClass("\077\111\116\111\114")P.MaxVelocity=(x.RPM.Value/50000)*(SHAKE_VALUE/10)end if TIMING_BELT and p~=nil then for P,g in pairs(p.Beams:GetChildren())do g.TextureSpeed=(x.RPM.Value/1000)*BELT_SPEED end end end end)local c=x.Boost.Changed:connect(function()if E then U:FireServer("\066\111\111\115\116",x.Boost.Value,g,f,TURBO_CHARGED,TURBO_BLUR,TURBO_SPEED)else if TURBO_CHARGED and f~=nil then for P,U in pairs(f:GetChildren())do if string.sub(U.Name,1,5)=="\084\117\114\098\111"then local P=U.W:FindFirstChildOfClass("\077\111\116\111\114")P.DesiredAngle=9000000000.0 if math.floor(x.Boost.Value)*TURBO_SPEED==0 then P.MaxVelocity=0 else P.MaxVelocity=x.Boost.Value*TURBO_SPEED end if TURBO_BLUR and U.Parts:FindFirstChild("\077\111\116\105\111\110\066\108\117\114")then U.Parts.MotionBlur.Blur.Transparency=1-((x.Boost.Value/g.T_Boost)/2)*.9 end end end end end end)local H=script.Parent.IsOn.Changed:Connect(function()if E then U:FireServer("\068\105\115\097\098\108\101",script.Parent.IsOn.Value,s,CAM_GEARS,ENGINE_SHAKE,MANIFOLD_HEAT,THROTTLE_BODY,TIMING_BELT,TURBO_CHARGED)else if script.Parent.IsOn.Value==true then CAM_GEARS=true ENGINE_SHAKE=true MANIFOLD_HEAT=true THROTTLE_BODY=true TIMING_BELT=true TURBO_CHARGED=true else CAM_GEARS=false ENGINE_SHAKE=false MANIFOLD_HEAT=false THROTTLE_BODY=false TIMING_BELT=false TURBO_CHARGED=false for P,x in pairs(s:GetDescendants())do if x:IsA("\077\111\116\111\114")then x.MaxVelocity=0 end if x:IsA("\066\101\097\109")then x.TextureSpeed=0 end end end end end)P.DriveSeat.ChildRemoved:Connect(function(P)if P.Name=="\083\101\097\116\087\101\108\100"and LEAVE_DISABLE then if E then U:FireServer("\068\105\115\097\098\108\101",false,s,CAM_GEARS,ENGINE_SHAKE,MANIFOLD_HEAT,THROTTLE_BODY,TIMING_BELT,TURBO_CHARGED)else CAM_GEARS=false ENGINE_SHAKE=false MANIFOLD_HEAT=false THROTTLE_BODY=false TIMING_BELT=false TURBO_CHARGED=false for P,x in pairs(s:GetDescendants())do if x:IsA("\077\111\116\111\114")then x.MaxVelocity=0 end if x:IsA("\066\101\097\109")then x.TextureSpeed=0 end end end end end)if E then U:FireServer("\076\111\111\112",y,w,p,ENGINE_SHAKE,MANIFOLD_HEAT,TIMING_BELT,SHAKE_ANGLE,MH_THRESHOLD,MH_COOLDOWN,BELT_TEXTURE,BELT_WIDTH)else spawn(function()if ENGINE_SHAKE and y~=nil then y.W.Motor.DesiredAngle=math.rad(SHAKE_ANGLE)end if TIMING_BELT and p~=nil then for P,x in pairs(p.Beams:GetChildren())do if x:IsA("\066\101\097\109")then x.Texture="\114\098\120\097\115\115\101\116\105\100\058\047\047"..BELT_TEXTURE x.Width0=BELT_WIDTH x.Width1=BELT_WIDTH end end end while wait()do if ENGINE_SHAKE and y~=nil then y.W.Motor.DesiredAngle=-y.W.Motor.DesiredAngle end if MANIFOLD_HEAT and w~=nil then if x.RPM.Value>MH_THRESHOLD then r=math.max(math.min(r+x.RPM.Value/900000,1),0)for P,x in pairs(w.Parts:GetDescendants())do if x:IsA("\066\097\115\101\080\097\114\116")then x.Color=Color3.fromRGB(110*r+60,45*r+40,40-40*r)end end elseif x.RPM.Value<MH_THRESHOLD then r=math.max(math.min(r+-(MH_COOLDOWN/1000),1),0)for P,x in pairs(w.Parts:GetDescendants())do if x:IsA("\066\097\115\101\080\097\114\116")then x.Color=Color3.fromRGB(110*r+60,45*r+40,40-40*r)end end end end end end)end


local Car		= script.Parent.Car.Value
local Values 	= script.Parent.Values
local _Tune 	= require(Car["A-Chassis Tune"])
local Handler	= Car:WaitForChild("ENGINE_PACK")
local FE		= workspace.FilteringEnabled

local ENG_PCK = nil
for _,Child in pairs(Car.Misc:GetDescendants()) do
	if Child:IsA("ObjectValue") and Child.Name == "ENGINE_PACK" then ENG_PCK = Child.Value end
end

local CG = ENG_PCK:FindFirstChild("Cam_Gears")
local ES = ENG_PCK:FindFirstChild("Engine_Shake")
local MH = ENG_PCK:FindFirstChild("Manifold_Heat")
local TB = ENG_PCK:FindFirstChild("Throttle_Body")
local TI = ENG_PCK:FindFirstChild("Timing_Belt")
local TC = ENG_PCK:FindFirstChild("Turbochargers")

local VAL = 0

local ThrottleChanged = Values.Throttle.Changed:Connect(function()
	if FE then
		Handler:FireServer("Throttle",Values.Throttle.Value,TB,THROTTLE_BODY)
	else
		
		if THROTTLE_BODY and TB ~= nil then
			for i,Body in pairs(TB:GetChildren()) do
				if string.sub(Body.Name,1,4) == "Body" then
					local Motor = Body.W:FindFirstChildOfClass("Motor")
					Motor.DesiredAngle = Values.Throttle.Value * 1.5
				end
			end
		end
		
	end
end)

local RPMChanged = Values.RPM.Changed:Connect(function()

	if FE then
		Handler:FireServer("RPM",Values.RPM.Value,CG,ES,TI,CAM_GEARS,ENGINE_SHAKE,TIMING_BELT,GEAR_SPEED,SHAKE_VALUE,BELT_SPEED)
	else
		
		if CAM_GEARS and CG ~= nil then
			for i,Gear in pairs(CG:GetChildren()) do
				if string.sub(Gear.Name,1,4) == "Gear" then
					local Motor = Gear.W:FindFirstChildOfClass("Motor")
					Motor.DesiredAngle = 9e9
					Motor.MaxVelocity = Values.RPM.Value/1000 * GEAR_SPEED
				end
			end
		end
		
		if ENGINE_SHAKE and ES ~= nil then
			local Motor = ES.W:FindFirstChildOfClass("Motor")
			Motor.MaxVelocity = Values.RPM.Value/50000 * (SHAKE_VALUE/10)
		end
		
		if TIMING_BELT and TI ~= nil then
			for i,Beam in pairs(TI.Beams:GetChildren()) do
				Beam.TextureSpeed = Values.RPM.Value/1000 * BELT_SPEED
			end
		end
		
	end
end)

local BoostChanged = Values.Boost.Changed:connect(function()
	
	if FE then
		Handler:FireServer("Boost",Values.Boost.Value,_Tune,TC,TURBO_CHARGED,TURBO_BLUR,TURBO_SPEED)
	else
		
		if TURBO_CHARGED and TC ~= nil then
			for i,Turbo in pairs(TC:GetChildren()) do
				if string.sub(Turbo.Name,1,5) == "Turbo" then
					local Motor = Turbo.W:FindFirstChildOfClass("Motor")
					Motor.DesiredAngle = 9e9
					if math.floor(Values.Boost.Value) * TURBO_SPEED == 0 then
						Motor.MaxVelocity = 0
					else
						Motor.MaxVelocity = Values.Boost.Value * TURBO_SPEED
					end
					if TURBO_BLUR and Turbo.Parts:FindFirstChild("MotionBlur") then
						Turbo.Parts.MotionBlur.Blur.Transparency = 1 - (Values.Boost.Value/_Tune.T_Boost/2*0.9)
					end
				end
			end
		end
		
	end
end)

local Disable = script.Parent.IsOn.Changed:Connect(function()
	if FE then Handler:FireServer("Disable",script.Parent.IsOn.Value,ENG_PCK,CAM_GEARS,ENGINE_SHAKE,MANIFOLD_HEAT,THROTTLE_BODY,TIMING_BELT,TURBO_CHARGED)
	else
		if script.Parent.IsOn.Value == true then
			CAM_GEARS		= true
			ENGINE_SHAKE 	= true
			MANIFOLD_HEAT	= true
			THROTTLE_BODY	= true
			TIMING_BELT		= true
			TURBO_CHARGED	= true
		else
			CAM_GEARS		= false
			ENGINE_SHAKE 	= false
			MANIFOLD_HEAT	= false
			THROTTLE_BODY	= false
			TIMING_BELT		= false
			TURBO_CHARGED	= false
			for i,Object in pairs(ENG_PCK:GetDescendants()) do
				if Object:IsA("Motor") then
					Object.MaxVelocity = 0
				end
				if Object:IsA("Beam") then
					Object.TextureSpeed = 0
				end
			end
		end
	end
end)

Car.DriveSeat.ChildRemoved:Connect(function(Child)
	if Child.Name == "SeatWeld" and LEAVE_DISABLE then
		if FE then
			Handler:FireServer("Disable",false,ENG_PCK,CAM_GEARS,ENGINE_SHAKE,MANIFOLD_HEAT,THROTTLE_BODY,TIMING_BELT,TURBO_CHARGED)
		else
			CAM_GEARS		= false
			ENGINE_SHAKE 	= false
			MANIFOLD_HEAT	= false
			THROTTLE_BODY	= false
			TIMING_BELT		= false
			TURBO_CHARGED	= false
			for i,Object in pairs(ENG_PCK:GetDescendants()) do
				if Object:IsA("Motor") then
					Object.MaxVelocity = 0
				end
				if Object:IsA("Beam") then
					Object.TextureSpeed = 0
				end
			end
		end
	end
end)

if FE then
	Handler:FireServer("Loop",ES,MH,TI,ENGINE_SHAKE,MANIFOLD_HEAT,TIMING_BELT,SHAKE_ANGLE,MH_THRESHOLD,MH_COOLDOWN,BELT_TEXTURE,BELT_WIDTH)
else
	spawn(function()

		if ENGINE_SHAKE and ES ~= nil then
			ES.W.Motor.DesiredAngle = math.rad(SHAKE_ANGLE)
		end
		
		if TIMING_BELT and TI ~= nil then
			for i,Beam in pairs(TI.Beams:GetChildren()) do
				if Beam:IsA("Beam") then
					Beam.Texture = "rbxassetid://"..BELT_TEXTURE
					Beam.Width0 = BELT_WIDTH
					Beam.Width1 = BELT_WIDTH
				end
			end
		end
		
		while wait() do

			if ENGINE_SHAKE and ES ~= nil then
				ES.W.Motor.DesiredAngle = -(ES.W.Motor.DesiredAngle)
			end

			if MANIFOLD_HEAT and MH ~= nil then
				if Values.RPM.Value > MH_THRESHOLD then
					VAL = math.max(math.min(VAL + Values.RPM.Value/900000,1),0)
					for i,ManifoldPart in pairs(MH.Parts:GetDescendants()) do
						if ManifoldPart:IsA("BasePart") then
							ManifoldPart.Color = Color3.fromRGB((110*VAL)+60,(45*VAL)+40,40-(40*VAL))
						end
					end
				elseif Values.RPM.Value < MH_THRESHOLD then
					VAL = math.max(math.min(VAL + -(MH_COOLDOWN/1000),1),0)
					for i,ManifoldPart in pairs(MH.Parts:GetDescendants()) do
						if ManifoldPart:IsA("BasePart") then
							ManifoldPart.Color = Color3.fromRGB((110*VAL)+60,(45*VAL)+40,40-(40*VAL))
						end
					end
				end
			end

		end
	end)
end
