function Gamepad.GetHighestPriorityGamepad(e)local l=UserInputService:GetConnectedGamepads()local Z=NONE for e,l in pairs(l)do if l.Value<Z.Value then Z=l end end return Z end function Gamepad.BindContextActions(e)if e.activeGamepad==NONE then return false end local l=function(l,Z,j)e.isJumping=Z==Enum.UserInputState.Begin return Enum.ContextActionResult.Sink end local Z=function(l,Z,j)if Z==Enum.UserInputState.Cancel then e.moveVector=ZERO_VECTOR3 return Enum.ContextActionResult.Sink end if e.activeGamepad~=j.UserInputType then return Enum.ContextActionResult.Pass end if j.KeyCode~=Enum.KeyCode.Thumbstick1 then return end if j.Position.magnitude>thumbstickDeadzone then e.moveVector=Vector3.new(j.Position.X,0,-j.Position.Y)else e.moveVector=ZERO_VECTOR3 end return Enum.ContextActionResult.Sink end ContextActionService:BindActivate(e.activeGamepad,Enum.KeyCode.ButtonR2)ContextActionService:BindActionAtPriority("\106\117\109\112\065\099\116\105\111\110",l,false,e.CONTROL_ACTION_PRIORITY,Enum.KeyCode.ButtonA)ContextActionService:BindActionAtPriority("\109\111\118\101\084\104\117\109\098\115\116\105\099\107",Z,false,e.CONTROL_ACTION_PRIORITY,Enum.KeyCode.Thumbstick1)return true end function Gamepad.UnbindContextActions(e)if e.activeGamepad~=NONE then ContextActionService:UnbindActivate(e.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(e)local l=e:GetHighestPriorityGamepad()if l==e.activeGamepad then return end if l==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")e:UnbindContextActions()return end if e.activeGamepad~=NONE then e:UnbindContextActions()end e.activeGamepad=l e:BindContextActions()end function Gamepad.OnCurrentGamepadDisconnected(e)if e.activeGamepad~=NONE then ContextActionService:UnbindActivate(e.activeGamepad,Enum.KeyCode.ButtonR2)end local l=e:GetHighestPriorityGamepad()if e.activeGamepad~=NONE and l==e.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")e:UnbindContextActions()e.activeGamepad=NONE return end if l==NONE then e:UnbindContextActions()e.activeGamepad=NONE else e.activeGamepad=l ContextActionService:BindActivate(e.activeGamepad,Enum.KeyCode.ButtonR2)end end function Gamepad.ConnectGamepadConnectionListeners(e)e.gamepadConnectedConn=UserInputService.GamepadConnected:Connect(function(l)e:OnNewGamepadConnected()end)e.gamepadDisconnectedConn=UserInputService.GamepadDisconnected:Connect(function(l)if e.activeGamepad==l then e:OnCurrentGamepadDisconnected()end end)end function Gamepad.DisconnectGamepadConnectionListeners(e)if e.gamepadConnectedConn then e.gamepadConnectedConn:Disconnect()e.gamepadConnectedConn=nil end if e.gamepadDisconnectedConn then e.gamepadDisconnectedConn:Disconnect()e.gamepadDisconnectedConn=nil end end return 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
