local T=_Tune.FinalDrive*_Tune.FDMult local S=(T*30)/math.pi local v=(workspace.Gravity*_Tune.InclineComp)/32.2 local t=(wDia*math.pi)/60 local a=CFrame.Angles(math.pi/2,-math.pi/2,0)local u=CFrame.Angles(0,math.pi,0)local R=.70710678118655 if _Tune.Aspiration=="\083\105\110\103\108\101"then _TCount=1 _TPsi=_Tune.Boost elseif _Tune.Aspiration=="\068\111\117\098\108\101"then _TCount=2 _TPsi=_Tune.Boost*2 end local w=_Tune.Horsepower/100 local l=(((_Tune.Horsepower*(_TPsi*(_Tune.CompressRatio/10)))/7.5)/2)/100 local d=_Tune.PeakRPM/1000 local A=_Tune.PeakSharpness local j=_Tune.CurveMult local m=_Tune.EqPoint/1000 function curveHP(T)T=T/1000 return(-(T-d)^2*math.min(w/d^2,j^(d/w))+w)*(T-T^A/(A*d^(A-1)))end local B=curveHP(_Tune.PeakRPM)function curvePSI(T)T=T/1000 return(-(T-d)^2*math.min(l/d^2,j^(d/l))+l)*(T-T^A/(A*d^(A-1)))end local I=curvePSI(_Tune.PeakRPM)function GetCurve(S,v)local t=math.max(curveHP(S)/(B/w),0)*100 return t,((((t*(m/S))*_Tune.Ratios[v+2])*T)*hpScaling)*1000 end function GetPsiCurve(S,v)local t=math.max(curvePSI(S)/(I/l),0)*100 return t,((((t*(m/S))*_Tune.Ratios[v+2])*T)*hpScaling)*1000 end local G={}local C={}if _Tune.CurveCache then for T,S in pairs(_Tune.Ratios)do local v={}local t={}for S=math.floor(_Tune.IdleRPM/_Tune.CacheInc),math.ceil((_Tune.Redline+100)/_Tune.CacheInc),1 do local a={}local u={}a.Horsepower,a.Torque=GetCurve(S*_Tune.CacheInc,T-2)u.Horsepower,u.Torque=GetPsiCurve(S*_Tune.CacheInc,T-2)hp1,tq1=GetCurve((S+1)*_Tune.CacheInc,T-2)hp2,tq2=GetPsiCurve((S+1)*_Tune.CacheInc,T-2)a.HpSlope=hp1-a.Horsepower u.HpSlope=hp2-u.Horsepower a.TqSlope=tq1-a.Torque u.TqSlope=tq2-u.Torque v[S]=a t[S]=u end table.insert(G,v)table.insert(C,t)end end if _Tune.CurveCache then for T,S in pairs(_Tune.Ratios)do for T=math.floor(_Tune.IdleRPM/_Tune.CacheInc),math.ceil((_Tune.Redline+100)/_Tune.CacheInc),1 do end end end wait()function RPM()if _CGear==0 then _ClutchOn=false end local T=_Tune.IdleRPM if not _IsOn then T=0 _CGear=0 _ClutchOn=false _GThrot=_Tune.IdleThrottle/100 end local v=0 for T,S in pairs(Drive)do if S.RotVelocity.Magnitude>v then v=S.RotVelocity.Magnitude end end if _ClutchOn then local t=math.max(math.min((v*_Tune.Ratios[_CGear+2])*S,_Tune.Redline+100),T)local a=math.min(math.abs(t-_RPM)/_Tune.ClutchTol,.93)_RPM=_RPM*a+t*(1-a)else if _GThrot-_Tune.IdleThrottle/100>0 then if _RPM>_Tune.Redline then _RPM=_RPM-_Tune.RevBounce*2 else _RPM=math.min(_RPM+_Tune.RevAccel*_GThrot,_Tune.Redline+100)end else _RPM=math.max(_RPM-_Tune.RevDecay,T)end end _spLimit=(_Tune.Redline+100)/(S*_Tune.Ratios[_CGear+2])if _RPM>_Tune.Redline then if _CGear<#_Tune.Ratios-2 then _RPM=_RPM-_Tune.RevBounce else _RPM=_RPM-_Tune.RevBounce*.5 end end local t=_TPsi/_TCount if _CGear==0 then _Boost=_Boost+(((((_RPM*((_GThrot*_GThrotShift)*1.2))/_Tune.Redline)/8.5-(_Boost/t)*(t/15))*((36/_Tune.TurboSize)*2))/t)*15 else _Boost=_Boost+(((((_HP*((_GThrot*_GThrotShift)*1.2))/_Tune.Horsepower)/8-(_Boost/t)*(t/15))*((36/_Tune.TurboSize)*2))/t)*15 end if _Boost<.05 then _Boost=.05 elseif _Boost>2 then _Boost=2 end end function Auto()local v=0 for T,S in pairs(Drive)do if S.RotVelocity.Magnitude>v then v=S.RotVelocity.Magnitude end end if _IsOn then _ClutchOn=true if _CGear>=1 then if _CGear==1 and(_GBrake>0 and car.DriveSeat.Velocity.Magnitude<5)then _CGear=1 else if _Tune.AutoShiftMode=="\082\080\077"then if _RPM>_Tune.PeakRPM+_Tune.AutoUpThresh then if _CGear~=0 and _CGear~=#_Tune.Ratios-2 then end _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif math.max(math.min((v*_Tune.Ratios[_CGear+1])*S,_Tune.Redline+100),_Tune.IdleRPM)<_Tune.PeakRPM-_Tune.AutoDownThresh then if _CGear~=1 then end _CGear=math.max(_CGear-1,1)end else if car.DriveSeat.Velocity.Magnitude>math.ceil(((t*(_Tune.PeakRPM+_Tune.AutoUpThresh))/_Tune.Ratios[_CGear+2])/T)then if _CGear~=0 and _CGear~=#_Tune.Ratios-2 then end _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif car.DriveSeat.Velocity.Magnitude<math.ceil(((t*(_Tune.PeakRPM-_Tune.AutoDownThresh))/_Tune.Ratios[_CGear+1])/T)then if _CGear~=1 then end _CGear=math.max(_CGear-1,1)end end end end end end local x=1 function Engine()if _ClutchOn then if _Tune.CurveCache then local T=G[_CGear+2][math.floor(math.min(_Tune.Redline,math.max(_Tune.IdleRPM,_RPM))/_Tune.CacheInc)]_NH=T.Horsepower+(T.HpSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000))%1 _NT=T.Torque+(T.TqSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000))%1 else _NH,_NT=GetCurve(_RPM,_CGear)end if _Tune.Aspiration~="\078\097\116\117\114\097\108"then if _Tune.CurveCache then local T=C[_CGear+2][math.floor(math.min(_Tune.Redline,math.max(_Tune.IdleRPM,_RPM))/_Tune.CacheInc)]_BH=T.Horsepower+(T.HpSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000))%1 _BT=T.Torque+(T.TqSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000))%1 else _BH,_BT=GetPsiCurve(_RPM,_CGear)end _HP=_NH+_BH*(_Boost/2)_OutTorque=_NT+_BT*(_Boost/2)else _HP=_NH _OutTorque=_NT end local T=car.DriveSeat.CFrame.lookVector.y*v if _CGear==-1 then T=-T end _OutTorque=_OutTorque*math.max(1,1+T)else _HP,_NH,_BH,_OutTorque,_NT,_BT=0,0,0,0,0,0 end local T=_TPsi/_TCount if _Boost<.05 then _Boost=.05 elseif _Boost>2 then _Boost=2 end local S=0 local t=0 local w=0 local l=0 for T,v in pairs(car.Wheels:GetChildren())do if v.Name=="\070\076"or v.Name=="\070\082"or v.Name=="\070"then S=S+v.RotVelocity.Magnitude t=t+1 elseif v.Name=="\082\076"or v.Name=="\082\082"or v.Name=="\082"then w=w+v.RotVelocity.Magnitude l=l+1 end end S=S/t w=w/l local d=(S+w)/2 for T,v in pairs(car.Wheels:GetChildren())do local t=(CFrame.new(v.Position-(v.Arm.CFrame*a).lookVector,v.Position)*u).lookVector local l=1 local A=1 if v.Name=="\070\076"or v.Name=="\082\076"then l=-1 end if _Tune.Config=="\065\087\068"then _OutTorque=_OutTorque*R end if v.Name=="\070\076"or v.Name=="\070\082"then A=math.max(0,math.min(1,1+(((v.RotVelocity.Magnitude-S)/S)/(math.max(_Tune.FDiffSlipThres,1)/100))*((_Tune.FDiffLockThres-50)/50)))if _Tune.Config=="\065\087\068"then A=math.max(0,math.min(1,A*(1+(((S-d)/d)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50))))end elseif v.Name=="\082\076"or v.Name=="\082\082"then A=math.max(0,math.min(1,1+(((v.RotVelocity.Magnitude-w)/w)/(math.max(_Tune.RDiffSlipThres,1)/100))*((_Tune.RDiffLockThres-50)/50)))if _Tune.Config=="\065\087\068"then A=math.max(0,math.min(1,A*(1+(((w-d)/d)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50))))end end _TCSActive=false _ABSActive=false if _PBrake and(_Tune.Config~="\070\087\068"and((v.Name=="\070\076"or v.Name=="\070\082")and car.DriveSeat.Velocity.Magnitude<20 or(v.Name=="\082\082"or v.Name=="\082\076")and car.DriveSeat.Velocity.Magnitude>=20)or _Tune.Config=="\070\087\068"and(v.Name=="\082\082"or v.Name=="\082\076"))then v["\035\065\086"].maxTorque=Vector3.new(math.abs(t.x),math.abs(t.y),math.abs(t.z))*PBrakeForce v["\035\065\086"].angularvelocity=Vector3.new()else if(_TMode=="\077\097\110\117\097\108"or _TMode=="\083\101\109\105"or _TMode=="\065\117\116\111")and _GBrake==0 then local T=false for S,t in pairs(Drive)do if t==v then T=true end end if T then local T=1 if not script.Parent.IsOn.Value then T=0 end local S=_GThrot*_GThrotShift if _TCS then x=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/100)end if x<1 then _TCSAmt=x _TCSActive=true end local a=1 if _CGear==-1 then a=-1 end v["\035\065\086"].maxTorque=(((((Vector3.new(math.abs(t.x),math.abs(t.y),math.abs(t.z))*_OutTorque)*(1+(v.RotVelocity.Magnitude/60)^1.15))*S)*x)*A)*T v["\035\065\086"].angularvelocity=((t*l)*_spLimit)*a else v["\035\065\086"].maxTorque=Vector3.new()v["\035\065\086"].angularvelocity=Vector3.new()end else local T=_GBrake local S=1 if _ABS and math.abs(v.RotVelocity.Magnitude*(v.Size.x/2)-v.Velocity.Magnitude)-_Tune.ABSThreshold>0 then S=0 end if S<1 then _ABSActive=true end if v.Name=="\070\076"or v.Name=="\070\082"or v.Name=="\070"then v["\035\065\086"].maxTorque=((Vector3.new(math.abs(t.x),math.abs(t.y),math.abs(t.z))*FBrakeForce)*T)*S else v["\035\065\086"].maxTorque=((Vector3.new(math.abs(t.x),math.abs(t.y),math.abs(t.z))*RBrakeForce)*T)*S end v["\035\065\086"].angularvelocity=Vector3.new()end end end end


	local fFD = _Tune.FinalDrive*_Tune.FDMult
	local fFDr = fFD*30/math.pi
	local cGrav = workspace.Gravity*_Tune.InclineComp/32.2
	local wDRatio = wDia*math.pi/60
	local cfWRot = CFrame.Angles(math.pi/2,-math.pi/2,0)
	local cfYRot = CFrame.Angles(0,math.pi,0)
	local rtTwo = (2^.5)/2
	
	if _Tune.Aspiration == "Single" then
		_TCount = 1
		_TPsi = _Tune.Boost
	elseif _Tune.Aspiration == "Double" then
		_TCount = 2
		_TPsi = _Tune.Boost*2
	end

	--Horsepower Curve
	
	
	local HP=_Tune.Horsepower/100
	local HP_B=((_Tune.Horsepower*((_TPsi)*(_Tune.CompressRatio/10))/7.5)/2)/100
	local Peak=_Tune.PeakRPM/1000
	local Sharpness=_Tune.PeakSharpness
	local CurveMult=_Tune.CurveMult
	local EQ=_Tune.EqPoint/1000
	
	--Horsepower Curve
	
	function curveHP(RPM)
		RPM=RPM/1000
		return ((-(RPM-Peak)^2)*math.min(HP/(Peak^2),CurveMult^(Peak/HP))+HP)*(RPM-((RPM^Sharpness)/(Sharpness*Peak^(Sharpness-1))))
	end
	
	local PeakCurveHP = curveHP(_Tune.PeakRPM)
	
	function curvePSI(RPM)
		RPM=RPM/1000
		return ((-(RPM-Peak)^2)*math.min(HP_B/(Peak^2),CurveMult^(Peak/HP_B))+HP_B)*(RPM-((RPM^Sharpness)/(Sharpness*Peak^(Sharpness-1))))
	end
	local PeakCurvePSI = curvePSI(_Tune.PeakRPM)
	
	--Plot Current Horsepower
	function GetCurve(x,gear)
		local hp=(math.max(curveHP(x)/(PeakCurveHP/HP),0))*100
		return hp,((hp*(EQ/x))*_Tune.Ratios[gear+2]*fFD*hpScaling)*1000
	end
	
	--Plot Current Boost (addition to Horsepower)
	function GetPsiCurve(x,gear)
		local hp=(math.max(curvePSI(x)/(PeakCurvePSI/HP_B),0))*100
		return hp,((hp*(EQ/x))*_Tune.Ratios[gear+2]*fFD*hpScaling)*1000
	end	
	
	--Output Cache	
	local HPCache = {}
	local PSICache = {}
	
	if _Tune.CurveCache then
		for gear,ratio in pairs(_Tune.Ratios) do
			local nhpPlot = {}
			local bhpPlot = {}
			for rpm = math.floor(_Tune.IdleRPM/_Tune.CacheInc),math.ceil((_Tune.Redline+100)/_Tune.CacheInc) do
				local ntqPlot = {}
				local btqPlot = {}
				ntqPlot.Horsepower,ntqPlot.Torque = GetCurve(rpm*_Tune.CacheInc,gear-2)
				btqPlot.Horsepower,btqPlot.Torque = GetPsiCurve(rpm*_Tune.CacheInc,gear-2)
				hp1,tq1 = GetCurve((rpm+1)*_Tune.CacheInc,gear-2)
				hp2,tq2 = GetPsiCurve((rpm+1)*_Tune.CacheInc,gear-2)
				ntqPlot.HpSlope = (hp1 - ntqPlot.Horsepower)
				btqPlot.HpSlope = (hp2 - btqPlot.Horsepower)
				ntqPlot.TqSlope = (tq1 - ntqPlot.Torque)
				btqPlot.TqSlope = (tq2 - btqPlot.Torque)
				nhpPlot[rpm] = ntqPlot
				bhpPlot[rpm] = btqPlot
			end
			table.insert(HPCache,nhpPlot)
			table.insert(PSICache,bhpPlot)
		end
	end
	
	
	if _Tune.CurveCache then
		for gear,ratio in pairs(_Tune.Ratios) do
			for rpm = math.floor(_Tune.IdleRPM/_Tune.CacheInc),math.ceil((_Tune.Redline+100)/_Tune.CacheInc) do
				
			end
		end
	end
	
	--Powertrain
	wait()

	--Update RPM
	function RPM()
		--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/100
		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]*fFDr,_Tune.Redline+100),revMin)
			local clutchP = math.min(math.abs(aRPM-_RPM)/_Tune.ClutchTol,.93)
			_RPM = _RPM*clutchP  +  aRPM*(1-clutchP)
		else
			if _GThrot-(_Tune.IdleThrottle/100)>0 then
				if _RPM>_Tune.Redline then
					_RPM = _RPM-_Tune.RevBounce*2
				else
					_RPM = math.min(_RPM+_Tune.RevAccel*_GThrot,_Tune.Redline+100)
				end
			else
				_RPM = math.max(_RPM-_Tune.RevDecay,revMin)
			end
		end
		
		--Rev Limiter
		_spLimit = (_Tune.Redline+100)/(fFDr*_Tune.Ratios[_CGear+2])
		if _RPM>_Tune.Redline then 
			if _CGear<#_Tune.Ratios-2 then
				_RPM = _RPM-_Tune.RevBounce
			else
				_RPM = _RPM-_Tune.RevBounce*.5
			end
		end
		
		local TPsi = _TPsi/_TCount
		if _CGear==0 then 
			_Boost = _Boost + ((((((_RPM*(_GThrot*_GThrotShift*1.2)/_Tune.Redline)/8.5)-(((_Boost/TPsi*(TPsi/15)))))*((36/_Tune.TurboSize)*2))/TPsi)*15)
		else
			_Boost = _Boost + ((((((_HP*(_GThrot*_GThrotShift*1.2)/_Tune.Horsepower)/8)-(((_Boost/TPsi*(TPsi/15)))))*((36/_Tune.TurboSize)*2))/TPsi)*15)
		end
		if _Boost < 0.05 then _Boost = 0.05 elseif _Boost > 2 then _Boost = 2 end
	end
	
	--Automatic Transmission
	function Auto()
		local maxSpin=0
		for i,v in pairs(Drive) do if v.RotVelocity.Magnitude>maxSpin then maxSpin = v.RotVelocity.Magnitude end end
		if _IsOn then
			_ClutchOn = true
			if _CGear >= 1 then
				if _CGear==1 and _GBrake > 0  and car.DriveSeat.Velocity.Magnitude < 5 then
					_CGear = 1
				else
					if _Tune.AutoShiftMode == "RPM" then
						if _RPM>(_Tune.PeakRPM+_Tune.AutoUpThresh) then
							if (_CGear ~= 0) and (_CGear ~= #_Tune.Ratios-2) then
							end
							_CGear=math.min(_CGear+1,#_Tune.Ratios-2) 
						elseif math.max(math.min(maxSpin*_Tune.Ratios[_CGear+1]*fFDr,_Tune.Redline+100),_Tune.IdleRPM)<(_Tune.PeakRPM-_Tune.AutoDownThresh) then
							if _CGear ~= 1 then
							end
							_CGear=math.max(_CGear-1,1)
						end
					else
						if car.DriveSeat.Velocity.Magnitude > math.ceil(wDRatio*(_Tune.PeakRPM+_Tune.AutoUpThresh)/_Tune.Ratios[_CGear+2]/fFD) then
							if (_CGear ~= 0) and (_CGear ~= #_Tune.Ratios-2) then
							end
							_CGear=math.min(_CGear+1,#_Tune.Ratios-2)
						elseif car.DriveSeat.Velocity.Magnitude < math.ceil(wDRatio*(_Tune.PeakRPM-_Tune.AutoDownThresh)/_Tune.Ratios[_CGear+1]/fFD) then
							if _CGear ~= 1 then
							end
							_CGear=math.max(_CGear-1,1)
						end
					end
				end
			end
		end 
	end
	
	local tqTCS = 1
	--Apply Power
	function Engine()
		if _ClutchOn then
			if _Tune.CurveCache then
				local cTq = HPCache[_CGear+2][math.floor(math.min(_Tune.Redline,math.max(_Tune.IdleRPM,_RPM))/_Tune.CacheInc)]
				_NH = cTq.Horsepower+(cTq.HpSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000)%1)
				_NT = cTq.Torque+(cTq.TqSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000)%1)
			else
				_NH,_NT = GetCurve(_RPM,_CGear)
			end
			if _Tune.Aspiration ~= "Natural" then
				if _Tune.CurveCache then
					local bTq = PSICache[_CGear+2][math.floor(math.min(_Tune.Redline,math.max(_Tune.IdleRPM,_RPM))/_Tune.CacheInc)]
					_BH = bTq.Horsepower+(bTq.HpSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000)%1)
					_BT = bTq.Torque+(bTq.TqSlope*((_RPM-math.floor(_RPM/_Tune.CacheInc))/1000)%1)
				else 
					_BH,_BT = GetPsiCurve(_RPM,_CGear)
				end
				_HP = _NH + (_BH*(_Boost/2))
				_OutTorque = _NT + (_BT*(_Boost/2))
			else
				_HP = _NH
				_OutTorque = _NT
			end
			
			local iComp =(car.DriveSeat.CFrame.lookVector.y)*cGrav
			if _CGear==-1 then iComp=-iComp end
			_OutTorque = _OutTorque*math.max(1,(1+iComp))
		else
			_HP,_NH,_BH,_OutTorque,_NT,_BT = 0,0,0,0,0,0
		end
		
		local TPsi = _TPsi/_TCount
		
		if _Boost < 0.05 then _Boost = 0.05 elseif _Boost > 2 then _Boost = 2 end
		
		--Average Rotational Speed Calculation
		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
		
		--Update Wheels
		for i,v in pairs(car.Wheels:GetChildren()) do
			--Reference Wheel Orientation
			local Ref=(CFrame.new(v.Position-((v.Arm.CFrame*cfWRot).lookVector),v.Position)*cfYRot).lookVector
			local aRef=1
			local diffMult=1
			if v.Name=="FL" or v.Name=="RL" then aRef=-1 end
			
			--AWD Torque Scaling
			if _Tune.Config == "AWD" then _OutTorque = _OutTorque*rtTwo end
			
			--Differential/Torque-Vectoring
			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
			
			_TCSActive = false
			_ABSActive = false
			--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
				--PBrake
				v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*PBrakeForce
				v["#AV"].angularvelocity=Vector3.new()
			else
				--Apply Power
				if ((_TMode == "Manual" or _TMode == "Semi" or _TMode == "Auto") and _GBrake==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 * _GThrotShift
						
						--Apply TCS
						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/100)))
						end
						if tqTCS < 1 then
							_TCSAmt = tqTCS
							_TCSActive = true
						end
						
						--Update Forces
						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
					
				--Brakes
				else
					local brake = _GBrake
					
					--Apply ABS
					local tqABS = 1
					if _ABS and math.abs(v.RotVelocity.Magnitude*(v.Size.x/2) - v.Velocity.Magnitude)-_Tune.ABSThreshold>0 then
						tqABS = 0
					end
					if tqABS < 1 then
						_ABSActive = true
					end
					
					--Update Forces
					if v.Name=="FL" or v.Name=="FR" or v.Name=="F" then
						v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*FBrakeForce*brake*tqABS
					else
						v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*RBrakeForce*brake*tqABS
					end
					v["#AV"].angularvelocity=Vector3.new()
				end
			end
		end
	end
	
	
	