--[[Run]]

	--Print Version
	local ver=require(car["A-Chassis Tune"].README)
	print("//INSPARE: AC6 Loaded - Build "..ver)
	
	--Runtime Loops
	
	-- ~60 c/s
	game["Run Service"].Stepped:connect(function()
		--Steering
		Steering()
		
		--RPM
		RPM()
		
		--Update External Values
		_IsOn = car.DriveSeat.IsOn.Value
		_InControls = script.Parent.ControlsOpen.Value
		script.Parent.Values.Gear.Value = _CGear
		script.Parent.Values.RPM.Value = _RPM
		script.Parent.Values.Horsepower.Value = _HP
		script.Parent.Values.Torque.Value = _HP * _Tune.EqPoint / _RPM
		script.Parent.Values.TransmissionMode.Value = _TMode
		script.Parent.Values.Throttle.Value = _GThrot
		script.Parent.Values.Brake.Value = _GBrake
		script.Parent.Values.SteerC.Value = _GSteerC*(1-math.min(car.DriveSeat.Velocity.Magnitude/_Tune.SteerDecay,1-(_Tune.MinSteer/100)))
		script.Parent.Values.SteerT.Value = _GSteerT
		script.Parent.Values.PBrake.Value = _PBrake
		script.Parent.Values.TCS.Value = _TCS
		script.Parent.Values.TCSActive.Value = _TCSActive
		script.Parent.Values.ABS.Value = _ABS
		script.Parent.Values.ABSActive.Value = _ABSActive
		script.Parent.Values.MouseSteerOn.Value = _MSteer
		script.Parent.Values.Velocity.Value = car.DriveSeat.Velocity
		
	
		if car.DriveSeat.ABS.Value == true and car.DriveSeat.AS.Value == false then	
			if car.DriveSeat.Velocity.Magnitude < 80 then
				if workspace:FindPartOnRay(Ray.new(snsrs.FS.RayEnd.CFrame.p,(snsrs.FS.RayEnd.CFrame.p-snsrs.FS.RayStart.CFrame.p).unit * ((car.DriveSeat.Velocity.Magnitude^1.05)*(10/12) * (60/88)))) and (_CGear > 1) then
					local p = workspace:FindPartOnRay(Ray.new(snsrs.FS.RayEnd.CFrame.p,(snsrs.FS.RayEnd.CFrame.p-snsrs.FS.RayStart.CFrame.p).unit * ((car.DriveSeat.Velocity.Magnitude^1.1)*(10/12) * (60/88))))
					local aa = math.abs(p.Velocity.Magnitude - car.DriveSeat.Velocity.Magnitude)
					if aa > 35 and (p.CanCollide == false or p.CanCollide == true or p.Parent.Name == "Body" or p.Parent.Parent.Name == "Body" or p.Parent.Name == "Wheels") and (p.Name ~= "Baseplate") and (p.Anchored == false) then
						if (p.Velocity.Magnitude < 5 or not p.Anchored) then
							car.DriveSeat.AS.Value = true
							_GBrake = 5
							repeat wait() until car.DriveSeat.Velocity.Magnitude < 2
							car.DriveSeat.AS.Value = false
						end
					end
				end
			end
		end		
		
		local mph = (10/12) * (60/88)
		if script.Parent.CC.Value == true then
			if (car.DriveSeat.Velocity.Magnitude*mph) - 2 > script.Parent.CCS.Value+0.5 then
				_CThrot = 0
				_CBrake = 0.5
				--repeat wait() until (car.DriveSeat.Velocity.Magnitude*mph) - 4 < script.Parent.CCS.Value
				--_CBrake = 0
			else
				_CThrot = 1
				_CBrake = 0
			end
		else
			_CThrot = 0
		end
		
		if _GBrake > 0 then
			script.Parent.CC.Value = false
		end		
		if car.DriveSeat.IsOn.Value == true and car.A.ACC.Value == true then
			_CGear = 1
		end		
	end)
	--script.Parent.CC.Changed:connect(function()
	--	if script.Parent.CC.Value == true then
		--	script.Parent.CCS.Value = math.floor(car.DriveSeat.Velocity.Magnitude*((10/12) * (60/88)))
		--end
	--end)

	script.Parent.Values.Brake.Changed:connect(function()
		if math.ceil(_GBrake) > 0 then
        	car.DriveSeat.FE_Lights:FireServer('updateLights', 'brake', true)
		else
        	car.DriveSeat.FE_Lights:FireServer('updateLights', 'brake', false)
		end
	end)

	script.Parent.Values.Throttle.Changed:Connect(function()
		if math.ceil(_GThrot) > 0 then
			if car.DriveSeat.AS.Value == true then
				car.DriveSeat.AS.Value = false
				_GBrake = 0
			end
		end
	end)
	
	-- ~15 c/s
	while wait(.0667) do
		--Power
		Engine()
		
		--Flip
		if _Tune.AutoFlip then Flip() end
	end