--Input Handler

function DealWithInput(input,IsRobloxFunction)
	if (UserInputService:GetFocusedTextBox()==nil) and not _InControls then --Ignore when UI Focus
		--Toggle PBrake
		if _IsOn and input.KeyCode ==_CTRL["ContlrPBrake"] or 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
		
		--Throttle
		elseif _IsOn and (input.KeyCode==_CTRL["Throttle"] or input.KeyCode == _CTRL["Throttle2"]) then
			if input.UserInputState == Enum.UserInputState.Begin then _GThrot = 1 else _GThrot = 0 end
			
		--Brake
		elseif (input.KeyCode==_CTRL["Brake"] or input.KeyCode == _CTRL["Brake2"]) then
			if input.UserInputState == Enum.UserInputState.Begin then _GBrake = 1 else _GBrake = 0 end
			
		--Steer Left
		elseif (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 (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
		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(.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(.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 _IsOn and input.KeyCode == _CTRL["ContlrThrottle"] then
				_GThrot = input.Position.Z
				
			--Gamepad Brake
			elseif input.KeyCode == _CTRL["ContlrBrake"] then
				_GBrake = input.Position.Z
			end
		end
	else
		_GThrot = 0
		_GSteerT = 0
		_GBrake = 0
	end
end
UserInputService.InputBegan:connect(DealWithInput)
UserInputService.InputChanged:connect(DealWithInput)
UserInputService.InputEnded:connect(DealWithInput)

while wait() do
	script.Parent.Values.Throttle.Value = _GThrot
	script.Parent.Values.Brake.Value = _GBrake
	script.Parent.Values.SteerT.Value = _GSteerT
	script.Parent.Values.PBrake.Value = _PBrake
end