function Gamepad.GetHighestPriorityGamepad(R)local C=UserInputService:GetConnectedGamepads()local w=NONE for R,C in pairs(C)do if C.Value<w.Value then w=C end end return w end function Gamepad.BindContextActions(R)if R.activeGamepad==NONE then return false end local C=function(C,w,A)R.isJumping=w==Enum.UserInputState.Begin return Enum.ContextActionResult.Sink end local w=function(C,w,A)if w==Enum.UserInputState.Cancel then R.moveVector=ZERO_VECTOR3 return Enum.ContextActionResult.Sink end if R.activeGamepad~=A.UserInputType then return Enum.ContextActionResult.Pass end if A.KeyCode~=Enum.KeyCode.Thumbstick1 then return end if A.Position.magnitude>thumbstickDeadzone then R.moveVector=Vector3.new(A.Position.X,0,-A.Position.Y)else R.moveVector=ZERO_VECTOR3 end return Enum.ContextActionResult.Sink end ContextActionService:BindActivate(R.activeGamepad,Enum.KeyCode.ButtonR2)ContextActionService:BindActionAtPriority("\106\117\109\112\065\099\116\105\111\110",C,false,R.CONTROL_ACTION_PRIORITY,Enum.KeyCode.ButtonA)ContextActionService:BindActionAtPriority("\109\111\118\101\084\104\117\109\098\115\116\105\099\107",w,false,R.CONTROL_ACTION_PRIORITY,Enum.KeyCode.Thumbstick1)return true end function Gamepad.UnbindContextActions(R)if R.activeGamepad~=NONE then ContextActionService:UnbindActivate(R.activeGamepad,Enum.KeyCode.ButtonR2)end ContextActionService:UnbindAction("\109\111\118\101\084\104\117\109\098\115\116\105\099\107")ContextActionService:UnbindAction("\106\117\109\112\065\099\116\105\111\110")end function Gamepad.OnNewGamepadConnected(R)local C=R:GetHighestPriorityGamepad()if C==R.activeGamepad then return end if C==NONE then warn("\071\097\109\101\112\097\100\058\079\110\078\101\119\071\097\109\101\112\097\100\067\111\110\110\101\099\116\101\100 \102\111\117\110\100 \110\111 \099\111\110\110\101\099\116\101\100 \103\097\109\101\112\097\100\115")R:UnbindContextActions()return end if R.activeGamepad~=NONE then R:UnbindContextActions()end R.activeGamepad=C R:BindContextActions()end function Gamepad.OnCurrentGamepadDisconnected(R)if R.activeGamepad~=NONE then ContextActionService:UnbindActivate(R.activeGamepad,Enum.KeyCode.ButtonR2)end local C=R:GetHighestPriorityGamepad()if R.activeGamepad~=NONE and C==R.activeGamepad then warn("\071\097\109\101\112\097\100\058\079\110\067\117\114\114\101\110\116\071\097\109\101\112\097\100\068\105\115\099\111\110\110\101\099\116\101\100 \102\111\117\110\100 \116\104\101 \115\117\112\112\111\115\101\100\108\121 \100\105\115\099\111\110\110\101\099\116\101\100 \103\097\109\101\112\097\100 \105\110 \099\111\110\110\101\099\116\101\100\071\097\109\101\112\097\100\115\046")R:UnbindContextActions()R.activeGamepad=NONE return end if C==NONE then R:UnbindContextActions()R.activeGamepad=NONE else R.activeGamepad=C ContextActionService:BindActivate(R.activeGamepad,Enum.KeyCode.ButtonR2)end end function Gamepad.ConnectGamepadConnectionListeners(R)R.gamepadConnectedConn=UserInputService.GamepadConnected:Connect(function(C)R:OnNewGamepadConnected()end)R.gamepadDisconnectedConn=UserInputService.GamepadDisconnected:Connect(function(C)if R.activeGamepad==C then R:OnCurrentGamepadDisconnected()end end)end function Gamepad.DisconnectGamepadConnectionListeners(R)if R.gamepadConnectedConn then R.gamepadConnectedConn:Disconnect()R.gamepadConnectedConn=nil end if R.gamepadDisconnectedConn then R.gamepadDisconnectedConn:Disconnect()R.gamepadDisconnectedConn=nil end end return Gamepad
-- This function selects the lowest number gamepad from the currently-connected gamepad
-- and sets it as the active gamepad
function Gamepad:GetHighestPriorityGamepad()
	local connectedGamepads = UserInputService:GetConnectedGamepads()
	local bestGamepad = NONE -- Note that this value is higher than all valid gamepad values
	for _, gamepad in pairs(connectedGamepads) do
		if gamepad.Value < bestGamepad.Value then
			bestGamepad = gamepad
		end
	end
	return bestGamepad
end

function Gamepad:BindContextActions()
	if self.activeGamepad == NONE then
		-- There must be an active gamepad to set up bindings
		return false
	end

	local handleJumpAction = function(actionName, inputState, inputObject)
		self.isJumping = (inputState == Enum.UserInputState.Begin)
		return Enum.ContextActionResult.Sink
	end

	local handleThumbstickInput = function(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Cancel then
			self.moveVector = ZERO_VECTOR3
			return Enum.ContextActionResult.Sink
		end

		if self.activeGamepad ~= inputObject.UserInputType then
			return Enum.ContextActionResult.Pass
		end
		if inputObject.KeyCode ~= Enum.KeyCode.Thumbstick1 then
			return
		end

		if inputObject.Position.magnitude > thumbstickDeadzone then
			self.moveVector = Vector3.new(inputObject.Position.X, 0, -inputObject.Position.Y)
		else
			self.moveVector = ZERO_VECTOR3
		end
		return Enum.ContextActionResult.Sink
	end

	ContextActionService:BindActivate(self.activeGamepad, Enum.KeyCode.ButtonR2)
	ContextActionService:BindActionAtPriority(
		"jumpAction",
		handleJumpAction,
		false,
		self.CONTROL_ACTION_PRIORITY,
		Enum.KeyCode.ButtonA
	)
	ContextActionService:BindActionAtPriority(
		"moveThumbstick",
		handleThumbstickInput,
		false,
		self.CONTROL_ACTION_PRIORITY,
		Enum.KeyCode.Thumbstick1
	)

	return true
end

function Gamepad:UnbindContextActions()
	if self.activeGamepad ~= NONE then
		ContextActionService:UnbindActivate(self.activeGamepad, Enum.KeyCode.ButtonR2)
	end
	ContextActionService:UnbindAction("moveThumbstick")
	ContextActionService:UnbindAction("jumpAction")
end

function Gamepad:OnNewGamepadConnected()
	-- A new gamepad has been connected.
	local bestGamepad = self:GetHighestPriorityGamepad()

	if bestGamepad == self.activeGamepad then
		-- A new gamepad was connected, but our active gamepad is not changing
		return
	end

	if bestGamepad == NONE then
		-- There should be an active gamepad when GamepadConnected fires, so this should not
		-- normally be hit. If there is no active gamepad, unbind actions but leave
		-- the module enabled and continue to listen for a new gamepad connection.
		warn("Gamepad:OnNewGamepadConnected found no connected gamepads")
		self:UnbindContextActions()
		return
	end

	if self.activeGamepad ~= NONE then
		-- Switching from one active gamepad to another
		self:UnbindContextActions()
	end

	self.activeGamepad = bestGamepad
	self:BindContextActions()
end

function Gamepad:OnCurrentGamepadDisconnected()
	if self.activeGamepad ~= NONE then
		ContextActionService:UnbindActivate(self.activeGamepad, Enum.KeyCode.ButtonR2)
	end

	local bestGamepad = self:GetHighestPriorityGamepad()

	if self.activeGamepad ~= NONE and bestGamepad == self.activeGamepad then
		warn("Gamepad:OnCurrentGamepadDisconnected found the supposedly disconnected gamepad in connectedGamepads.")
		self:UnbindContextActions()
		self.activeGamepad = NONE
		return
	end

	if bestGamepad == NONE then
		-- No active gamepad, unbinding actions but leaving gamepad connection listener active
		self:UnbindContextActions()
		self.activeGamepad = NONE
	else
		-- Set new gamepad as active and bind to tool activation
		self.activeGamepad = bestGamepad
		ContextActionService:BindActivate(self.activeGamepad, Enum.KeyCode.ButtonR2)
	end
end

function Gamepad:ConnectGamepadConnectionListeners()
	self.gamepadConnectedConn = UserInputService.GamepadConnected:Connect(function(gamepadEnum)
		self:OnNewGamepadConnected()
	end)

	self.gamepadDisconnectedConn = UserInputService.GamepadDisconnected:Connect(function(gamepadEnum)
		if self.activeGamepad == gamepadEnum then
			self:OnCurrentGamepadDisconnected()
		end
	end)
end

function Gamepad:DisconnectGamepadConnectionListeners()
	if self.gamepadConnectedConn then
		self.gamepadConnectedConn:Disconnect()
		self.gamepadConnectedConn = nil
	end

	if self.gamepadDisconnectedConn then
		self.gamepadDisconnectedConn:Disconnect()
		self.gamepadDisconnectedConn = nil
	end
end

return Gamepad
