local l=_Tune.Controls local c=Instance.new("\070\111\108\100\101\114",script.Parent)c.Name="\067\111\110\116\114\111\108\115"for g,A in pairs(l)do local n=Instance.new("\083\116\114\105\110\103\086\097\108\117\101",c)n.Name=g n.Value=A.Name n.Changed:connect(function()if g=="\077\111\117\115\101\084\104\114\111\116\116\108\101"or g=="\077\111\117\115\101\066\114\097\107\101"then l[g]=Enum.UserInputType[n.Value]else l[g]=Enum.KeyCode[n.Value]end end)end function DealWithInput(c,g)if UserInputService:GetFocusedTextBox()==nil and not _InControls then if _IsOn and((c.KeyCode==l.ContlrShiftDown or _MSteer and c.KeyCode==l.MouseShiftDown or not _MSteer and c.KeyCode==l.ShiftDown)and(_TMode=="\077\097\110\117\097\108"and(not _ClutchOn and c.UserInputState==Enum.UserInputState.Begin)))then if _CGear==0 then _ClutchOn=true end _CGear=math.max(_CGear-1,-1)elseif _IsOn and((c.KeyCode==l.ContlrShiftUp or _MSteer and c.KeyCode==l.MouseShiftUp or not _MSteer and c.KeyCode==l.ShiftUp)and(_TMode=="\077\097\110\117\097\108"and(not _ClutchOn and c.UserInputState==Enum.UserInputState.Begin)))then if _CGear==0 then _ClutchOn=true end _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif _IsOn and((c.KeyCode==l.ContlrClutch or _MSteer and c.KeyCode==l.MouseClutch or not _MSteer and c.KeyCode==l.Clutch)and _TMode=="\077\097\110\117\097\108")then if c.UserInputState==Enum.UserInputState.Begin then _ClutchOn=false elseif c.UserInputState==Enum.UserInputState.End then _ClutchOn=true end elseif _IsOn and c.KeyCode==l.ContlrPBrake or _MSteer and c.KeyCode==l.MousePBrake or not _MSteer and c.KeyCode==l.PBrake then if c.UserInputState==Enum.UserInputState.Begin then _PBrake=not _PBrake elseif c.UserInputState==Enum.UserInputState.End then if car.DriveSeat.Velocity.Magnitude>5 then _PBrake=false end end elseif(c.KeyCode==l.ContlrToggleTMode or c.KeyCode==l.ToggleTransMode)and c.UserInputState==Enum.UserInputState.Begin then local l=1 for c,g in pairs(_Tune.TransModes)do if g==_TMode then l=c break end end l=l+1 if l>#_Tune.TransModes then l=1 end _TMode=_Tune.TransModes[l]elseif _IsOn and(not _MSteer and(c.KeyCode==l.Throttle or c.KeyCode==l.Throttle2))or c.UserInputType==l.MouseThrottle and _MSteer then if c.UserInputState==Enum.UserInputState.Begin then _GThrot=1 else _GThrot=_Tune.IdleThrottle end elseif not _MSteer and(c.KeyCode==l.Brake or c.KeyCode==l.Brake2)or c.UserInputType==l.MouseBrake and _MSteer then if c.UserInputState==Enum.UserInputState.Begin then _GBrake=1 else _GBrake=0 end elseif not _MSteer and(c.KeyCode==l.SteerLeft or c.KeyCode==l.SteerLeft2)then if c.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(c.KeyCode==l.SteerRight or c.KeyCode==l.SteerRight2)then if c.UserInputState==Enum.UserInputState.Begin then _GSteerT=1 _SteerR=true else if _SteerL then _GSteerT=-1 else _GSteerT=0 end _SteerR=false end elseif c.KeyCode==l.ToggleMouseDrive then if c.UserInputState==Enum.UserInputState.End then _MSteer=not _MSteer _GThrot=_Tune.IdleThrottle _GBrake=0 _GSteerT=0 _ClutchOn=true end elseif _IsOn and c.KeyCode==l.ToggleTCS or c.KeyCode==l.ContlrToggleTCS then if c.UserInputState==Enum.UserInputState.End then _TCS=not _TCS end end if c.UserInputType.Name:find("\071\097\109\101\112\097\100")then if c.KeyCode==l.ContlrSteer then if math.abs(c.Position.X)>_Tune.ControlDZone then _GSteerT=(math.abs(c.Position.X-_Tune.ControlDZone)*(c.Position.X/math.abs(c.Position.X)))/(1-_Tune.ControlDZone)else _GSteerT=0 end elseif _IsOn and c.KeyCode==l.ContlrThrottle then _GThrot=math.max(_Tune.IdleThrottle,c.Position.Z)elseif c.KeyCode==l.ContlrBrake then _GBrake=c.Position.Z end end else _GThrot=_Tune.IdleThrottle _GSteerT=0 _GBrake=0 if _CGear~=0 then _ClutchOn=true end end end UserInputService.InputBegan:connect(DealWithInput)UserInputService.InputChanged:connect(DealWithInput)UserInputService.InputEnded:connect(DealWithInput)

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
			_CTRL[i]=Enum.UserInputType[a.Value]
		else
			_CTRL[i]=Enum.KeyCode[a.Value]
		end
	end)
end
function DealWithInput(input,IsRobloxFunction)
	if (UserInputService:GetFocusedTextBox()==nil) and not _InControls then --No texting while driving
		if _IsOn and  (input.KeyCode ==_CTRL["ContlrShiftDown"] or (_MSteer and input.KeyCode==_CTRL["MouseShiftDown"]) or ((not _MSteer) and input.KeyCode==_CTRL["ShiftDown"])) and _TMode=="Manual" and (not _ClutchOn) and input.UserInputState == Enum.UserInputState.Begin then
			if _CGear == 0 then _ClutchOn = true end
			_CGear = math.max(_CGear-1,-1)
		elseif _IsOn and  (input.KeyCode ==_CTRL["ContlrShiftUp"] or (_MSteer and input.KeyCode==_CTRL["MouseShiftUp"]) or ((not _MSteer) and input.KeyCode==_CTRL["ShiftUp"])) and _TMode=="Manual" and (not _ClutchOn) and input.UserInputState == Enum.UserInputState.Begin then
			if _CGear == 0 then _ClutchOn = true end
			_CGear = math.min(_CGear+1,#_Tune.Ratios-2)
		elseif _IsOn and  (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
				_ClutchOn = false
			elseif input.UserInputState == Enum.UserInputState.End then
				_ClutchOn = true
			end
		elseif _IsOn and  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 car.DriveSeat.Velocity.Magnitude>5 then
					_PBrake = false
				end
			end
		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]
		elseif _IsOn and ((not _MSteer) and (input.KeyCode==_CTRL["Throttle"] or input.KeyCode == _CTRL["Throttle2"])) or (input.UserInputType == _CTRL["MouseThrottle"] and _MSteer) then
			if input.UserInputState == Enum.UserInputState.Begin then
				_GThrot = 1
			else
				_GThrot = _Tune.IdleThrottle
			end
		elseif ((not _MSteer) and (input.KeyCode==_CTRL["Brake"] or input.KeyCode == _CTRL["Brake2"])) or (input.UserInputType == _CTRL["MouseBrake"] and _MSteer) then
			if input.UserInputState == Enum.UserInputState.Begin then
				_GBrake = 1
			else
				_GBrake = 0
			end
		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
		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
		elseif input.KeyCode ==_CTRL["ToggleMouseDrive"] then
			if input.UserInputState == Enum.UserInputState.End then
				_MSteer = not _MSteer
				_GThrot = _Tune.IdleThrottle
				_GBrake = 0
				_GSteerT = 0
				_ClutchOn = true
			end
		elseif  _IsOn and input.KeyCode == _CTRL["ToggleTCS"] or input.KeyCode == _CTRL["ContlrToggleTCS"] then
			if input.UserInputState == Enum.UserInputState.End then
				_TCS = not _TCS
			end
		end
		if input.UserInputType.Name:find("Gamepad") then
			if input.KeyCode == _CTRL["ContlrSteer"] then
				if math.abs(input.Position.X)>_Tune.ControlDZone then
					_GSteerT = math.abs(input.Position.X-_Tune.ControlDZone)*(input.Position.X/math.abs(input.Position.X))/(1-_Tune.ControlDZone)
				else
					_GSteerT = 0
				end
			elseif _IsOn and input.KeyCode == _CTRL["ContlrThrottle"] then
				_GThrot = math.max(_Tune.IdleThrottle,input.Position.Z)
			elseif input.KeyCode == _CTRL["ContlrBrake"] then
				_GBrake = input.Position.Z
			end
		end
	else
		_GThrot = _Tune.IdleThrottle
		_GSteerT = 0
		_GBrake = 0
		if _CGear~=0 then _ClutchOn = true end
	end
end
UserInputService.InputBegan:connect(DealWithInput)
UserInputService.InputChanged:connect(DealWithInput)
UserInputService.InputEnded:connect(DealWithInput)
