local N=_Tune.Controls local R=Instance.new("\070\111\108\100\101\114",script.Parent)R.Name="\067\111\110\116\114\111\108\115"for s,v in pairs(N)do local g=Instance.new("\083\116\114\105\110\103\086\097\108\117\101",R)g.Name=s g.Value=v.Name g.Changed:connect(function()if s=="\077\111\117\115\101\084\104\114\111\116\116\108\101"or s=="\077\111\117\115\101\066\114\097\107\101"then if g.Value=="\077\111\117\115\101\066\117\116\116\111\110\049"or g.Value=="\077\111\117\115\101\066\117\116\116\111\110\050"then N[s]=Enum.UserInputType[g.Value]else N[s]=Enum.KeyCode[g.Value]end else N[s]=Enum.KeyCode[g.Value]end end)end local s=_Tune.Peripherals for N,v in pairs(s)do local g=Instance.new("\073\110\116\086\097\108\117\101",R)g.Name=N g.Value=v g.Changed:connect(function()g.Value=math.min(100,math.max(0,g.Value))s[N]=g.Value end)end function DealWithInput(R,s)if UserInputService:GetFocusedTextBox()==nil and not _InControls then if(R.KeyCode==N.ContlrShiftDown or _MSteer and R.KeyCode==N.MouseShiftDown or not _MSteer and R.KeyCode==N.ShiftDown)and((_IsOn and((_TMode=="\065\117\116\111"and _CGear<=1)and _Tune.AutoShiftVers=="\078\101\119")or _TMode=="\068\067\084"or _TMode=="\077\097\110\117\097\108")and R.UserInputState==Enum.UserInputState.Begin)then if not _ShiftDn then _ShiftDn=true end elseif(R.KeyCode==N.ContlrShiftUp or _MSteer and R.KeyCode==N.MouseShiftUp or not _MSteer and R.KeyCode==N.ShiftUp)and((_IsOn and((_TMode=="\065\117\116\111"and _CGear<1)and _Tune.AutoShiftVers=="\078\101\119")or _TMode=="\068\067\084"or _TMode=="\077\097\110\117\097\108")and R.UserInputState==Enum.UserInputState.Begin)then if not _ShiftUp then _ShiftUp=true end elseif(R.KeyCode==N.ContlrClutch or _MSteer and R.KeyCode==N.MouseClutch or not _MSteer and R.KeyCode==N.Clutch)and _TMode=="\077\097\110\117\097\108"then if R.UserInputState==Enum.UserInputState.Begin then _ClPressing=true elseif R.UserInputState==Enum.UserInputState.End then _ClPressing=false end elseif R.KeyCode==N.ContlrPBrake or _MSteer and R.KeyCode==N.MousePBrake or not _MSteer and R.KeyCode==N.PBrake then if R.UserInputState==Enum.UserInputState.Begin then _PBrake=not _PBrake elseif R.UserInputState==Enum.UserInputState.End then if bike.DriveSeat.Velocity.Magnitude>5 then _PBrake=false end end elseif(R.KeyCode==N.ContlrToggleTMode or R.KeyCode==N.ToggleTransMode)and R.UserInputState==Enum.UserInputState.Begin then local N=1 for R,s in pairs(_Tune.TransModes)do if s==_TMode then N=R break end end N=N+1 if N>#_Tune.TransModes then N=1 end _TMode=_Tune.TransModes[N]elseif not _MSteer and(R.KeyCode==N.Throttle or R.KeyCode==N.Throttle2)or((N.MouseThrottle==Enum.UserInputType.MouseButton1 or N.MouseThrottle==Enum.UserInputType.MouseButton2)and R.UserInputType==N.MouseThrottle or R.KeyCode==N.MouseThrottle)and _MSteer then if R.UserInputState==Enum.UserInputState.Begin and _IsOn then _InThrot=1 _TTick=tick()else _InThrot=_Tune.IdleThrottle/100 _BTick=tick()end elseif not _MSteer and(R.KeyCode==N.Brake or R.KeyCode==N.Brake2)or((N.MouseBrake==Enum.UserInputType.MouseButton1 or N.MouseBrake==Enum.UserInputType.MouseButton2)and R.UserInputType==N.MouseBrake or R.KeyCode==N.MouseBrake)and _MSteer then if R.UserInputState==Enum.UserInputState.Begin then _InBrake=1 else _InBrake=0 end elseif not _MSteer and(R.KeyCode==N.SteerLeft or R.KeyCode==N.SteerLeft2)then if R.UserInputState==Enum.UserInputState.Begin then _GSteerT=-1 _SteerL=true else if _SteerR then _GSteerT=1 else _GSteerT=0 end _SteerL=false end elseif not _MSteer and(R.KeyCode==N.SteerRight or R.KeyCode==N.SteerRight2)then if R.UserInputState==Enum.UserInputState.Begin then _GSteerT=1 _SteerR=true else if _SteerL then _GSteerT=-1 else _GSteerT=0 end _SteerR=false end elseif R.KeyCode==N.ToggleMouseDrive then if R.UserInputState==Enum.UserInputState.End then _MSteer=not _MSteer _InThrot=_Tune.IdleThrottle/100 _TTick=tick()_InBrake=0 _GSteerT=0 end elseif _Tune.TCS and(_IsOn and R.KeyCode==N.ToggleTCS)or R.KeyCode==N.ContlrToggleTCS then if R.UserInputState==Enum.UserInputState.End then _TCS=not _TCS end elseif _Tune.ABS and(_IsOn and R.KeyCode==N.ToggleABS)or R.KeyCode==N.ContlrToggleABS then if R.UserInputState==Enum.UserInputState.End then _ABS=not _ABS end end if R.UserInputType.Name:find("\071\097\109\101\112\097\100")then if R.KeyCode==N.ContlrSteer then if R.Position.X>=0 then local N=math.min(.99,_Tune.Peripherals.ControlRDZone/100)if math.abs(R.Position.X)>N then _GSteerT=(R.Position.X-N)/(1-N)else _GSteerT=0 end else local N=math.min(.99,_Tune.Peripherals.ControlLDZone/100)if math.abs(R.Position.X)>N then _GSteerT=(R.Position.X+N)/(1-N)else _GSteerT=0 end end elseif R.KeyCode==N.ContlrThrottle then if _IsOn then _InThrot=math.max(_Tune.IdleThrottle/100,R.Position.Z)if _InThrot>_Tune.IdleThrottle/100 then _TTick=tick()else _BTick=tick()end else _InThrot=_Tune.IdleThrottle/100 _BTick=tick()end elseif R.KeyCode==N.ContlrBrake then _InBrake=R.Position.Z end end else _InThrot=_Tune.IdleThrottle/100 _GSteerT=0 _InBrake=0 end end UserInputService.InputBegan:connect(DealWithInput)UserInputService.InputChanged:connect(DealWithInput)UserInputService.InputEnded:connect(DealWithInput)
--[[Controls]]

local _CTRL = _Tune.Controls
local Controls = Instance.new("Folder", script.Parent)
Controls.Name = "Controls"
for i, v in pairs(_CTRL) do
	local a = Instance.new("StringValue", Controls)
	a.Name = i
	a.Value = v.Name
	a.Changed:connect(function()
		if i == "MouseThrottle" or i == "MouseBrake" then
			if a.Value == "MouseButton1" or a.Value == "MouseButton2" then
				_CTRL[i] = Enum.UserInputType[a.Value]
			else
				_CTRL[i] = Enum.KeyCode[a.Value]
			end
		else
			_CTRL[i] = Enum.KeyCode[a.Value]
		end
	end)
end

--Deadzone Adjust
local _PPH = _Tune.Peripherals
for i, v in pairs(_PPH) do
	local a = Instance.new("IntValue", Controls)
	a.Name = i
	a.Value = v
	a.Changed:connect(function()
		a.Value = math.min(100, math.max(0, a.Value))
		_PPH[i] = a.Value
	end)
end

--Input Handler
function DealWithInput(input, IsRobloxFunction)
	if (UserInputService:GetFocusedTextBox() == nil) and not _InControls then --Ignore when UI Focus
		--Shift Down [Manual Transmission]
		if
			(
				input.KeyCode == _CTRL["ContlrShiftDown"]
				or (_MSteer and input.KeyCode == _CTRL["MouseShiftDown"])
				or ((not _MSteer) and input.KeyCode == _CTRL["ShiftDown"])
			)
			and ((_IsOn and ((_TMode == "Auto" and _CGear <= 1) and _Tune.AutoShiftVers == "New") or _TMode == "DCT") or _TMode == "Manual")
			and input.UserInputState == Enum.UserInputState.Begin
		then
			if not _ShiftDn then
				_ShiftDn = true
			end

			--Shift Up [Manual Transmission]
		elseif
			(
				input.KeyCode == _CTRL["ContlrShiftUp"]
				or (_MSteer and input.KeyCode == _CTRL["MouseShiftUp"])
				or ((not _MSteer) and input.KeyCode == _CTRL["ShiftUp"])
			)
			and ((_IsOn and ((_TMode == "Auto" and _CGear < 1) and _Tune.AutoShiftVers == "New") or _TMode == "DCT") or _TMode == "Manual")
			and input.UserInputState == Enum.UserInputState.Begin
		then
			if not _ShiftUp then
				_ShiftUp = true
			end

			--Toggle Clutch
		elseif
			(
				input.KeyCode == _CTRL["ContlrClutch"]
				or (_MSteer and input.KeyCode == _CTRL["MouseClutch"])
				or ((not _MSteer) and input.KeyCode == _CTRL["Clutch"])
			) and _TMode == "Manual"
		then
			if input.UserInputState == Enum.UserInputState.Begin then
				_ClPressing = true
			elseif input.UserInputState == Enum.UserInputState.End then
				_ClPressing = false
			end

			--Toggle PBrake
		elseif
			input.KeyCode == _CTRL["ContlrPBrake"]
			or (_MSteer and input.KeyCode == _CTRL["MousePBrake"])
			or ((not _MSteer) and input.KeyCode == _CTRL["PBrake"])
		then
			if input.UserInputState == Enum.UserInputState.Begin then
				_PBrake = not _PBrake
			elseif input.UserInputState == Enum.UserInputState.End then
				if bike.DriveSeat.Velocity.Magnitude > 5 then
					_PBrake = false
				end
			end

			--Toggle Transmission
		elseif
			(input.KeyCode == _CTRL["ContlrToggleTMode"] or input.KeyCode == _CTRL["ToggleTransMode"])
			and input.UserInputState == Enum.UserInputState.Begin
		then
			local n = 1
			for i, v in pairs(_Tune.TransModes) do
				if v == _TMode then
					n = i
					break
				end
			end
			n = n + 1
			if n > #_Tune.TransModes then
				n = 1
			end
			_TMode = _Tune.TransModes[n]

			--Throttle
		elseif
			((not _MSteer) and (input.KeyCode == _CTRL["Throttle"] or input.KeyCode == _CTRL["Throttle2"]))
			or (
				(
					(
						(
							_CTRL["MouseThrottle"] == Enum.UserInputType.MouseButton1
							or _CTRL["MouseThrottle"] == Enum.UserInputType.MouseButton2
						) and input.UserInputType == _CTRL["MouseThrottle"]
					) or input.KeyCode == _CTRL["MouseThrottle"]
				) and _MSteer
			)
		then
			if input.UserInputState == Enum.UserInputState.Begin and _IsOn then
				_InThrot = 1
				_TTick = tick()
			else
				_InThrot = _Tune.IdleThrottle / 100
				_BTick = tick()
			end

			--Brake
		elseif
			((not _MSteer) and (input.KeyCode == _CTRL["Brake"] or input.KeyCode == _CTRL["Brake2"]))
			or (
				(
					(
						(
							_CTRL["MouseBrake"] == Enum.UserInputType.MouseButton1
							or _CTRL["MouseBrake"] == Enum.UserInputType.MouseButton2
						) and input.UserInputType == _CTRL["MouseBrake"]
					) or input.KeyCode == _CTRL["MouseBrake"]
				) and _MSteer
			)
		then
			if input.UserInputState == Enum.UserInputState.Begin then
				_InBrake = 1
			else
				_InBrake = 0
			end

			--Steer Left
		elseif (not _MSteer) and (input.KeyCode == _CTRL["SteerLeft"] or input.KeyCode == _CTRL["SteerLeft2"]) then
			if input.UserInputState == Enum.UserInputState.Begin then
				_GSteerT = -1
				_SteerL = true
			else
				if _SteerR then
					_GSteerT = 1
				else
					_GSteerT = 0
				end
				_SteerL = false
			end

			--Steer Right
		elseif (not _MSteer) and (input.KeyCode == _CTRL["SteerRight"] or input.KeyCode == _CTRL["SteerRight2"]) then
			if input.UserInputState == Enum.UserInputState.Begin then
				_GSteerT = 1
				_SteerR = true
			else
				if _SteerL then
					_GSteerT = -1
				else
					_GSteerT = 0
				end
				_SteerR = false
			end

			--Toggle Mouse Controls
		elseif input.KeyCode == _CTRL["ToggleMouseDrive"] then
			if input.UserInputState == Enum.UserInputState.End then
				_MSteer = not _MSteer
				_InThrot = _Tune.IdleThrottle / 100
				_TTick = tick()
				_InBrake = 0
				_GSteerT = 0
			end

			--Toggle TCS
		elseif
			_Tune.TCS and _IsOn and input.KeyCode == _CTRL["ToggleTCS"] or input.KeyCode == _CTRL["ContlrToggleTCS"]
		then
			if input.UserInputState == Enum.UserInputState.End then
				_TCS = not _TCS
			end

			--Toggle ABS
		elseif
			_Tune.ABS and _IsOn and input.KeyCode == _CTRL["ToggleABS"] or input.KeyCode == _CTRL["ContlrToggleABS"]
		then
			if input.UserInputState == Enum.UserInputState.End then
				_ABS = not _ABS
			end
		end

		--Variable Controls
		if input.UserInputType.Name:find("Gamepad") then
			--Gamepad Steering
			if input.KeyCode == _CTRL["ContlrSteer"] then
				if input.Position.X >= 0 then
					local cDZone = math.min(0.99, _Tune.Peripherals.ControlRDZone / 100)
					if math.abs(input.Position.X) > cDZone then
						_GSteerT = (input.Position.X - cDZone) / (1 - cDZone)
					else
						_GSteerT = 0
					end
				else
					local cDZone = math.min(0.99, _Tune.Peripherals.ControlLDZone / 100)
					if math.abs(input.Position.X) > cDZone then
						_GSteerT = (input.Position.X + cDZone) / (1 - cDZone)
					else
						_GSteerT = 0
					end
				end

				--Gamepad Throttle
			elseif input.KeyCode == _CTRL["ContlrThrottle"] then
				if _IsOn then
					_InThrot = math.max(_Tune.IdleThrottle / 100, input.Position.Z)
					if _InThrot > _Tune.IdleThrottle / 100 then
						_TTick = tick()
					else
						_BTick = tick()
					end
				else
					_InThrot = _Tune.IdleThrottle / 100
					_BTick = tick()
				end

				--Gamepad Brake
			elseif input.KeyCode == _CTRL["ContlrBrake"] then
				_InBrake = input.Position.Z
			end
		end
	else
		_InThrot = _Tune.IdleThrottle / 100
		_GSteerT = 0
		_InBrake = 0
	end
end
UserInputService.InputBegan:connect(DealWithInput)
UserInputService.InputChanged:connect(DealWithInput)
UserInputService.InputEnded:connect(DealWithInput)
