UIS.InputBegan doesnt work for gamepad RT LT

A) My issue is input does not work for Button RT and Button LT. My goals is to make Button RT and LT work on InputBegan, but instead it only works on InputEnded.

B) In this image depicting output window and the results here are odd. The L2 and R2 are pressed and it always puts “End”

image

  • output covered with green box are results when holding and releasing the LT and RT button.
  • output covered with pink box are results when holding and releasing the button A.

C) I don’t think that I found a solution.

Control Module

type Binds = { Enum.KeyCode | Enum.UserInputType }

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local Trove = require(ReplicatedStorage.Codebase.Packages.Trove)
local GoodSignal = require(ReplicatedStorage.Codebase.Packages.GoodSignal)

local Controls = {}

Controls.__index = Controls

function Controls.new(options)
	local self = {
		Emitted = GoodSignal.new(),
		_trove = Trove.new(),
		_options = options,
	}

	local function inputHandler(inputObject: InputObject, focus: boolean)
		local input = inputObject.KeyCode

		if input == Enum.KeyCode.Unknown then
			input = inputObject.UserInputType
		end

		local state = inputObject.UserInputState

		if table.find(self._options.Binding, input) then
			self.Emitted:Fire(input, state, focus)
		-- 	print(true)
		-- else
		-- 	print(false)
		end
	end

	self._trove:Connect(UserInputService.InputBegan, inputHandler)
	self._trove:Connect(UserInputService.InputEnded, inputHandler)

	return self
end

function Controls.fromBinding(binds: Binds)
	return Controls.new({ Binding = binds })
end

return Controls

Main

	local Binding = {
		Aim = Input.Controls.fromBinding({ Enum.KeyCode.ButtonL2, Enum.KeyCode.ButtonR2 }),
		Test = Input.Controls.fromBinding({ Enum.KeyCode.ButtonA })
	}

	Binding.Test.Emitted:Connect(function(input, state, focus)
		print(input)
		print(state)
	end)

	Binding.Aim.Emitted:Connect(function(input, state, focus)
		print(input)
		print(state)
	end)

Fixed all by myself.

--[[
	Made by DryOfficial for project-solterra

	Controls is a crucial for input controller. this detects in any sort of inputs
	which include: mouse, keyboard, gamepad (console) and a phone which is just touchpad
]]

type Bind = Enum.KeyCode | Enum.UserInputType
type Binds = { Bind }

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local Trove = require(ReplicatedStorage.Codebase.Packages.Trove)
local GoodSignal = require(ReplicatedStorage.Codebase.Packages.GoodSignal)
local Cryo = require(ReplicatedStorage.Codebase.Packages.Cryo)

local Controls = {}

Controls.__index = Controls

function Controls.new(options)
	local self = {
		-- Events
		Emitted = GoodSignal.new(),
		Changed = GoodSignal.new(),

		-- Properties
		State = Enum.UserInputState.None,
		Keys = {} :: { [Bind]: boolean },
		Focus = false,

		-- Internal
		_trove = Trove.new(),
		_options = options,
	}

	--> Register all binding into states (is key pressed)
	for _, bind: Bind in options.Binding do
		self.Keys[bind.Name] = false
	end

	local function updateForEmit(areKeysDown)
		local state = areKeysDown and Enum.UserInputState.Begin or Enum.UserInputState.End

		self.OldState, self.State = self.State, state

		if self.State ~= self.OldState then
			self.Emitted:Fire(state, self.Focus)
		end
	end

	local function handleInput(inputObject: InputObject, isNotFocus)
		local input, state = inputObject.KeyCode, inputObject.UserInputState

		if input == Enum.KeyCode.Unknown then
			input = inputObject.UserInputType
		end

		if self.Keys[input.Name] ~= nil then
			if state == Enum.UserInputState.Begin or state == Enum.UserInputState.Change then
				self.Keys[input.Name] = true
			elseif state == Enum.UserInputState.End then
				self.Keys[input.Name] = false
			end

			self.Changed:Fire(self.Keys)

			self.Focus = not isNotFocus
		end
	end

	self.Changed:Connect(function(keys)
		local areKeysDown = false

		for _, bool in keys do
			if bool == true then
				areKeysDown = true
				break
			end
		end

		updateForEmit(areKeysDown)
	end)

	self._trove:Connect(UserInputService.InputBegan, handleInput)
	self._trove:Connect(UserInputService.InputEnded, handleInput)
	self._trove:Connect(UserInputService.InputChanged, handleInput)

	return self
end

function Controls.fromBinding(binds: Binds)
	return Controls.new({ Binding = binds })
end

return Controls

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.