--[[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 > 100 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.12)*(10/12) * (60/88)))) 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 or p.Parent.Name == "Body" or p.Parent.Parent.Name == "Body" or p.Parent.Name == "Wheels") and (p.Name ~= "Baseplate") then
						if (p.Velocity.Magnitude < 5 or p.Anchored) then
							car.DriveSeat.AS.Value = true
							_GBrake = 1
							repeat wait() until car.DriveSeat.Velocity.Magnitude < 2
							car.DriveSeat.AS.Value = false
						end
					end
				end
			else
				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)))) 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 or p.Parent.Name == "Body" or p.Parent.Parent.Name == "Body" or p.Parent.Name == "Wheels") and (p.Name ~= "Baseplate") then
						if (p.Velocity.Magnitude < 5 or p.Anchored) then
							car.DriveSeat.AS.Value = true
							_GBrake = 1
							repeat wait() until car.DriveSeat.Velocity.Magnitude < 2
							car.DriveSeat.AS.Value = false
						end
					end
				end
			end
			
			if workspace:FindPartOnRay(Ray.new(snsrs.BMSL.RayEnd.CFrame.p,(snsrs.BMSL.RayEnd.CFrame.p-snsrs.BMSL.RayStart.CFrame.p).unit * 6)) and (_CGear == -1) then
				local p = workspace:FindPartOnRay(Ray.new(snsrs.BMSL.RayEnd.CFrame.p,(snsrs.BMSL.RayEnd.CFrame.p-snsrs.BMSL.RayStart.CFrame.p).unit * 6))
				local aa = math.abs(p.Velocity.Magnitude - car.DriveSeat.Velocity.Magnitude)
				if aa > 5 and (p.CanCollide) then
					if (p.Velocity.Magnitude < 5 or p.Anchored) then
						car.DriveSeat.AS.Value = true
						_GBrake = 1
						repeat wait() until car.DriveSeat.Velocity.Magnitude < 2
						car.DriveSeat.AS.Value = false
					end
				end
			end
	
			if workspace:FindPartOnRay(Ray.new(snsrs.BMSR.RayEnd.CFrame.p,(snsrs.BMSR.RayEnd.CFrame.p-snsrs.BMSR.RayStart.CFrame.p).unit * 6)) and (_CGear == -1) then
				local p = workspace:FindPartOnRay(Ray.new(snsrs.BMSR.RayEnd.CFrame.p,(snsrs.BMSR.RayEnd.CFrame.p-snsrs.BMSR.RayStart.CFrame.p).unit * 6))
				local aa = math.abs(p.Velocity.Magnitude - car.DriveSeat.Velocity.Magnitude)
				if aa > 5 and (p.CanCollide) then
					if (p.Velocity.Magnitude < 5 or p.Anchored) then
						car.DriveSeat.AS.Value = true
						_GBrake = 1
						repeat wait() until car.DriveSeat.Velocity.Magnitude < 2
						car.DriveSeat.AS.Value = false
					end
				end
			end
	
			if workspace:FindPartOnRay(Ray.new(snsrs.RS.RayEnd.CFrame.p,(snsrs.RS.RayEnd.CFrame.p-snsrs.RS.RayStart.CFrame.p).unit * 6)) and (_CGear == -1) then
				local p = workspace:FindPartOnRay(Ray.new(snsrs.RS.RayEnd.CFrame.p,(snsrs.RS.RayEnd.CFrame.p-snsrs.RS.RayStart.CFrame.p).unit * 6))
				local aa = math.abs(p.Velocity.Magnitude - car.DriveSeat.Velocity.Magnitude)
				if aa > 5 and (p.CanCollide) then
					if (p.Velocity.Magnitude < 5 or p.Anchored) then
						car.DriveSeat.AS.Value = true
						_GBrake = 1
						repeat wait() until car.DriveSeat.Velocity.Magnitude < 2
						car.DriveSeat.AS.Value = false
					end
				end
			end
		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