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