function Engine()if _CGear==0 then _ClutchOn=false end local n=_Tune.IdleRPM if not _IsOn then n=0 _CGear=0 _ClutchOn=false _GThrot=_Tune.IdleThrottle end local u=0 for n,t in pairs(Drive)do if t.RotVelocity.Magnitude>u then u=t.RotVelocity.Magnitude end end if _ClutchOn then local t=math.max(math.min((((u*_Tune.Ratios[_CGear+2])*_Tune.FinalDrive)*30)/math.pi,_Tune.Redline+100),n)local C=math.min(math.abs(t-_RPM)/_Tune.ClutchTol,.9)_RPM=((_RPM*2)*C+(t*2)*(1-C))/2 _HP=(_Tune.Horsepower/2)*math.sin((math.pi/((1+math.min(10,_Tune.IdleOffset)/100)*_Tune.PeakRPM))*(_RPM-((2-(1+math.min(10,_Tune.IdleOffset)/100))*_Tune.PeakRPM)/2))+_Tune.Horsepower/2 _OutTorque=(((_HP*5250)/_RPM)*_Tune.Ratios[_CGear+2])*_Tune.FinalDrive else if _GThrot-_Tune.IdleThrottle>0 then _RPM=math.min(_RPM+_Tune.RevAccel*_GThrot,_Tune.Redline+100)else _RPM=math.max(_RPM-_Tune.RevDecay,n)end _OutTorque=0 end local t=0 if _RPM>_Tune.Redline then if _CGear<#_Tune.Ratios-2 then _RPM=_RPM-_Tune.RevBounce t=0 else _RPM=_RPM-_Tune.RevBounce*.5 end else t=((_Tune.Redline+100)*math.pi)/((30*_Tune.Ratios[_CGear+2])*_Tune.FinalDrive)end if _TMode=="\065\117\116\111"and _IsOn then _ClutchOn=true if _CGear==0 then _CGear=1 end if _CGear>=1 then if _CGear==1 and(_GBrake>0 and car.DriveSeat.Velocity.Magnitude<20)then _CGear=-1 else if _Tune.AutoShiftMode=="\082\080\077"then if _RPM>_Tune.PeakRPM+_Tune.AutoUpThresh then _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif math.max(math.min((((u*_Tune.Ratios[_CGear+1])*_Tune.FinalDrive)*30)/math.pi,_Tune.Redline+100),_Tune.IdleRPM)<_Tune.PeakRPM-_Tune.AutoDownThresh then _CGear=math.max(_CGear-1,1)end else if car.DriveSeat.Velocity.Magnitude>math.ceil(((((wDia*math.pi)*(_Tune.PeakRPM+_Tune.AutoUpThresh))/60)/_Tune.Ratios[_CGear+2])/_Tune.FinalDrive)then _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif car.DriveSeat.Velocity.Magnitude<math.ceil(((((wDia*math.pi)*(_Tune.PeakRPM-_Tune.AutoDownThresh))/60)/_Tune.Ratios[_CGear+1])/_Tune.FinalDrive)then _CGear=math.max(_CGear-1,1)end end end else if _GThrot-_Tune.IdleThrottle>0 and car.DriveSeat.Velocity.Magnitude<20 then _CGear=1 end end end local C=0 local U=0 local K=0 local Q=0 for n,u in pairs(car.Wheels:GetChildren())do if u.Name=="\070\076"or u.Name=="\070\082"or u.Name=="\070"then C=C+u.RotVelocity.Magnitude U=U+1 elseif u.Name=="\082\076"or u.Name=="\082\082"or u.Name=="\082"then K=K+u.RotVelocity.Magnitude Q=Q+1 end end C=C/U K=K/Q local Z=(C+K)/2 for n,u in pairs(car.Wheels:GetChildren())do local U=u.Axle.CFrame.lookVector local Q=1 local o=1 if u.Name=="\070\076"or u.Name=="\082\076"then Q=-1 end if _Tune.Config~="\065\087\068"then _OutTorque=_OutTorque*1.3 end if u.Name=="\070\076"or u.Name=="\070\082"then o=math.max(0,math.min(1,1+(((u.RotVelocity.Magnitude-C)/C)/(math.max(_Tune.FDiffSlipThres,1)/100))*((_Tune.FDiffLockThres-50)/50)))if _Tune.Config=="\065\087\068"then o=math.max(0,math.min(1,o*(1+(((C-Z)/Z)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50))))end elseif u.Name=="\082\076"or u.Name=="\082\082"then o=math.max(0,math.min(1,1+(((u.RotVelocity.Magnitude-K)/K)/(math.max(_Tune.RDiffSlipThres,1)/100))*((_Tune.RDiffLockThres-50)/50)))if _Tune.Config=="\065\087\068"then o=math.max(0,math.min(1,o*(1+(((K-Z)/Z)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50))))end end if _PBrake and(_Tune.Config~="\070\087\068"and((u.Name=="\070\076"or u.Name=="\070\082")and car.DriveSeat.Velocity.Magnitude<20 or(u.Name=="\082\082"or u.Name=="\082\076")and car.DriveSeat.Velocity.Magnitude>=20)or _Tune.Config=="\070\087\068"and(u.Name=="\082\082"or u.Name=="\082\076"))then u["\035\065\086"].maxTorque=Vector3.new(math.abs(U.x),math.abs(U.y),math.abs(U.z))*_Tune.PBrakeForce u["\035\065\086"].angularvelocity=Vector3.new()else if _TMode=="\077\097\110\117\097\108"and _GBrake==0 or _TMode=="\065\117\116\111"and(_CGear>-1 and _GBrake==0 or _CGear==-1 and _GThrot-_Tune.IdleThrottle==0)then local n=false for t,C in pairs(Drive)do if C==u then n=true end end if n then local n=1 if not script.Parent.IsOn.Value then n=0 end local C=_GThrot if _TMode=="\065\117\116\111"and _CGear==-1 then C=_GBrake end local K=1 if _TCS then K=1-math.min(math.max(0,math.abs(u.RotVelocity.Magnitude*(u.Size.x/2)-u.Velocity.Magnitude)-_Tune.TCSThreshold)/_Tune.TCSGradient,1)*(1-_Tune.TCSLimit)end if K<1 then _TCSActive=true else _TCSActive=false end local Z=1 if _CGear==-1 then Z=-1 end u["\035\065\086"].maxTorque=(((((Vector3.new(math.abs(U.x),math.abs(U.y),math.abs(U.z))*_OutTorque)*(1+(u.RotVelocity.Magnitude/60)^1.15))*C)*K)*o)*n u["\035\065\086"].angularvelocity=((U*Q)*t)*Z else u["\035\065\086"].maxTorque=Vector3.new()u["\035\065\086"].angularvelocity=Vector3.new()end else local n=_GBrake if _TMode=="\065\117\116\111"and _CGear==-1 then n=_GThrot end u["\035\065\086"].maxTorque=(Vector3.new(math.abs(U.x),math.abs(U.y),math.abs(U.z))*_Tune.BrakeForce)*n u["\035\065\086"].angularvelocity=Vector3.new()end end end end
--Engine
function Engine()
	--Neutral Gear
	if _CGear == 0 then
		_ClutchOn = false
	end

	--Car is off
	local revMin = _Tune.IdleRPM
	if not _IsOn then
		revMin = 0
		_CGear = 0
		_ClutchOn = false
		_GThrot = _Tune.IdleThrottle
	end

	--Determine RPM
	local maxSpin = 0
	for i, v in pairs(Drive) do
		if v.RotVelocity.Magnitude > maxSpin then
			maxSpin = v.RotVelocity.Magnitude
		end
	end
	if _ClutchOn then
		local aRPM = math.max(
			math.min(maxSpin * _Tune.Ratios[_CGear + 2] * _Tune.FinalDrive * 30 / math.pi, _Tune.Redline + 100),
			revMin
		)
		local clutchP = math.min(math.abs(aRPM - _RPM) / _Tune.ClutchTol, 0.9)
		_RPM = ((_RPM * 2 * clutchP) + (aRPM * 2 * (1 - clutchP))) / 2
		_HP = (_Tune.Horsepower / 2)
				* math.sin(
					(math.pi / ((1 + (math.min(10, _Tune.IdleOffset) / 100)) * _Tune.PeakRPM))
						* (_RPM - (((2 - (1 + (math.min(10, _Tune.IdleOffset) / 100))) * _Tune.PeakRPM) / 2))
				)
			+ (_Tune.Horsepower / 2)
		_OutTorque = _HP * 5250 / _RPM * _Tune.Ratios[_CGear + 2] * _Tune.FinalDrive
	else
		if _GThrot - _Tune.IdleThrottle > 0 then
			_RPM = math.min(_RPM + _Tune.RevAccel * _GThrot, _Tune.Redline + 100)
		else
			_RPM = math.max(_RPM - _Tune.RevDecay, revMin)
		end
		_OutTorque = 0
	end

	--Rev Limiter
	local spLimit = 0
	if _RPM > _Tune.Redline then
		if _CGear < #_Tune.Ratios - 2 then
			_RPM = _RPM - _Tune.RevBounce
			spLimit = 0
		else
			_RPM = _RPM - _Tune.RevBounce * 0.5
		end
	else
		spLimit = (_Tune.Redline + 100) * math.pi / (30 * _Tune.Ratios[_CGear + 2] * _Tune.FinalDrive)
	end

	--Automatic Transmission
	if _TMode == "Auto" and _IsOn then
		_ClutchOn = true
		if _CGear == 0 then
			_CGear = 1
		end
		if _CGear >= 1 then
			if _CGear == 1 and _GBrake > 0 and car.DriveSeat.Velocity.Magnitude < 20 then
				_CGear = -1
			else
				if _Tune.AutoShiftMode == "RPM" then
					if _RPM > (_Tune.PeakRPM + _Tune.AutoUpThresh) then
						_CGear = math.min(_CGear + 1, #_Tune.Ratios - 2)
					elseif
						math.max(
							math.min(
								maxSpin * _Tune.Ratios[_CGear + 1] * _Tune.FinalDrive * 30 / math.pi,
								_Tune.Redline + 100
							),
							_Tune.IdleRPM
						) < (_Tune.PeakRPM - _Tune.AutoDownThresh)
					then
						_CGear = math.max(_CGear - 1, 1)
					end
				else
					if
						car.DriveSeat.Velocity.Magnitude
						> math.ceil(
							wDia
								* math.pi
								* (_Tune.PeakRPM + _Tune.AutoUpThresh)
								/ 60
								/ _Tune.Ratios[_CGear + 2]
								/ _Tune.FinalDrive
						)
					then
						_CGear = math.min(_CGear + 1, #_Tune.Ratios - 2)
					elseif
						car.DriveSeat.Velocity.Magnitude
						< math.ceil(
							wDia
								* math.pi
								* (_Tune.PeakRPM - _Tune.AutoDownThresh)
								/ 60
								/ _Tune.Ratios[_CGear + 1]
								/ _Tune.FinalDrive
						)
					then
						_CGear = math.max(_CGear - 1, 1)
					end
				end
			end
		else
			if _GThrot - _Tune.IdleThrottle > 0 and car.DriveSeat.Velocity.Magnitude < 20 then
				_CGear = 1
			end
		end
	end

	--Differential Stuff
	local fwspeed = 0
	local fwcount = 0
	local rwspeed = 0
	local rwcount = 0

	for i, v in pairs(car.Wheels:GetChildren()) do
		if v.Name == "FL" or v.Name == "FR" or v.Name == "F" then
			fwspeed = fwspeed + v.RotVelocity.Magnitude
			fwcount = fwcount + 1
		elseif v.Name == "RL" or v.Name == "RR" or v.Name == "R" then
			rwspeed = rwspeed + v.RotVelocity.Magnitude
			rwcount = rwcount + 1
		end
	end
	fwspeed = fwspeed / fwcount
	rwspeed = rwspeed / rwcount
	local cwspeed = (fwspeed + rwspeed) / 2

	--Apply Forces
	for i, v in pairs(car.Wheels:GetChildren()) do
		local Ref = v.Axle.CFrame.lookVector
		local aRef = 1
		local diffMult = 1
		if v.Name == "FL" or v.Name == "RL" then
			aRef = -1
		end

		--Torque Compensation
		if _Tune.Config ~= "AWD" then
			_OutTorque = _OutTorque * 1.3
		end

		--Differential
		if v.Name == "FL" or v.Name == "FR" then
			diffMult = math.max(
				0,
				math.min(
					1,
					1
						+ (
							(
								((v.RotVelocity.Magnitude - fwspeed) / fwspeed) / (
									math.max(_Tune.FDiffSlipThres, 1) / 100
								)
							) * ((_Tune.FDiffLockThres - 50) / 50)
						)
				)
			)
			if _Tune.Config == "AWD" then
				diffMult = math.max(
					0,
					math.min(
						1,
						diffMult
							* (
								1
								+ (
									(((fwspeed - cwspeed) / cwspeed) / (math.max(_Tune.CDiffSlipThres, 1) / 100))
									* ((_Tune.CDiffLockThres - 50) / 50)
								)
							)
					)
				)
			end
		elseif v.Name == "RL" or v.Name == "RR" then
			diffMult = math.max(
				0,
				math.min(
					1,
					1
						+ (
							(
								((v.RotVelocity.Magnitude - rwspeed) / rwspeed) / (
									math.max(_Tune.RDiffSlipThres, 1) / 100
								)
							) * ((_Tune.RDiffLockThres - 50) / 50)
						)
				)
			)
			if _Tune.Config == "AWD" then
				diffMult = math.max(
					0,
					math.min(
						1,
						diffMult
							* (
								1
								+ (
									(((rwspeed - cwspeed) / cwspeed) / (math.max(_Tune.CDiffSlipThres, 1) / 100))
									* ((_Tune.CDiffLockThres - 50) / 50)
								)
							)
					)
				)
			end
		end

		--Output
		if
			_PBrake
			and (
				(
					_Tune.Config ~= "FWD"
					and (
						((v.Name == "FL" or v.Name == "FR") and car.DriveSeat.Velocity.Magnitude < 20)
						or ((v.Name == "RR" or v.Name == "RL") and car.DriveSeat.Velocity.Magnitude >= 20)
					)
				) or (_Tune.Config == "FWD" and (v.Name == "RR" or v.Name == "RL"))
			)
		then
			v["#AV"].maxTorque = Vector3.new(math.abs(Ref.x), math.abs(Ref.y), math.abs(Ref.z)) * _Tune.PBrakeForce
			v["#AV"].angularvelocity = Vector3.new()
		else
			if
				(_TMode == "Manual" and _GBrake == 0)
				or (
					_TMode == "Auto" and (
						(_CGear > -1 and _GBrake == 0) or (_CGear == -1 and _GThrot - _Tune.IdleThrottle == 0)
					)
				)
			then
				local driven = false
				for _, a in pairs(Drive) do
					if a == v then
						driven = true
					end
				end
				if driven then
					local on = 1
					if not script.Parent.IsOn.Value then
						on = 0
					end
					local throt = _GThrot
					if _TMode == "Auto" and _CGear == -1 then
						throt = _GBrake
					end
					local tqTCS = 1
					if _TCS then
						tqTCS = 1
							- (
								math.min(
									math.max(
										0,
										math.abs(v.RotVelocity.Magnitude * (v.Size.x / 2) - v.Velocity.Magnitude)
											- _Tune.TCSThreshold
									) / _Tune.TCSGradient,
									1
								) * (1 - _Tune.TCSLimit)
							)
					end
					if tqTCS < 1 then
						_TCSActive = true
					else
						_TCSActive = false
					end
					local dir = 1
					if _CGear == -1 then
						dir = -1
					end
					v["#AV"].maxTorque = Vector3.new(math.abs(Ref.x), math.abs(Ref.y), math.abs(Ref.z))
						* _OutTorque
						* (1 + (v.RotVelocity.Magnitude / 60) ^ 1.15)
						* throt
						* tqTCS
						* diffMult
						* on
					v["#AV"].angularvelocity = Ref * aRef * spLimit * dir
				else
					v["#AV"].maxTorque = Vector3.new()
					v["#AV"].angularvelocity = Vector3.new()
				end
			else
				local brake = _GBrake
				if _TMode == "Auto" and _CGear == -1 then
					brake = _GThrot
				end
				v["#AV"].maxTorque = Vector3.new(math.abs(Ref.x), math.abs(Ref.y), math.abs(Ref.z))
					* _Tune.BrakeForce
					* brake
				v["#AV"].angularvelocity = Vector3.new()
			end
		end
	end
end
