Is there any benefit?

Hi everyone! I am kind of new to scripting and was exploring the templates made by Roblox and came across the “InputCategorizer” script.

InputCategorizer script
local UserInputService = game:GetService("UserInputService")

local lastInputCategoryChangedEvent = Instance.new("BindableEvent")

type ActionCallback = (string, Enum.UserInputState, InputObject) -> ...any

local InputCategory = {
	KeyboardAndMouse = "KeyboardAndMouse",
	Gamepad = "Gamepad",
	Touch = "Touch",
	Unknown = "Unknown",
}

local InputCategorizer = {
	InputCategory = InputCategory,
	lastInputCategoryChanged = lastInputCategoryChangedEvent.Event,
	_lastInputCategory = InputCategory.Unknown,
	_initialized = false,
}

-- Return the last input category
function InputCategorizer.getLastInputCategory()
	return InputCategorizer._lastInputCategory
end

-- If _lastInputCategory and inputCategory are different, set _lastInputCategory and fire lastInputCategoryChanged
function InputCategorizer._setLastInputCategory(inputCategory: string)
	if InputCategorizer._lastInputCategory ~= inputCategory then
		InputCategorizer._lastInputCategory = inputCategory
		lastInputCategoryChangedEvent:Fire(inputCategory)
	end
end

-- Return an InputCategory based on the UserInputType
function InputCategorizer._getCategoryOfInputType(inputType: Enum.UserInputType)
	if string.find(inputType.Name, "Gamepad") then
		return InputCategory.Gamepad
	elseif inputType == Enum.UserInputType.Keyboard or string.find(inputType.Name, "Mouse") then
		return InputCategory.KeyboardAndMouse
	elseif inputType == Enum.UserInputType.Touch then
		return InputCategory.Touch
	else
		return InputCategory.Unknown
	end
end

function InputCategorizer._onInputTypeChanged(inputType: Enum.UserInputType)
	local inputCategory = InputCategorizer._getCategoryOfInputType(inputType)
	if inputCategory ~= InputCategory.Unknown then
		InputCategorizer._setLastInputCategory(inputCategory)
	end
end

-- Return a default input category based on the current peripherals
function InputCategorizer._getDefaultInputCategory()
	local lastInputType = UserInputService:GetLastInputType()
	local lastInputCategory = InputCategorizer._getCategoryOfInputType(lastInputType)

	if lastInputCategory ~= InputCategory.Unknown then
		return lastInputCategory
	end

	if UserInputService.KeyboardEnabled and UserInputService.MouseEnabled then
		return InputCategory.KeyboardAndMouse
	elseif UserInputService.TouchEnabled then
		return InputCategory.Touch
	elseif UserInputService.GamepadEnabled then
		return InputCategory.Gamepad
	else
		warn("No input devices detected!")
		return InputCategory.Unknown
	end
end

function InputCategorizer._initialize()
	assert(not InputCategorizer._initialized, "InputCategorizer already initialized!")

	-- Update the last category when the last inputType changes
	UserInputService.LastInputTypeChanged:Connect(InputCategorizer._onInputTypeChanged)

	-- Set the starting input category
	local defaultInputCategory = InputCategorizer._getDefaultInputCategory()
	InputCategorizer._setLastInputCategory(defaultInputCategory)

	InputCategorizer._initialized = true
end

InputCategorizer._initialize()

return InputCategorizer

What I wish to ask is, is there any benefit to using that versus the one that I created which except for the fact that is a lot more structured, I think does the same thing. Let me know on what I am missing!

My Input Change detector
local Uis = game:GetService("UserInputService")
local KeyboardandMouse = require(script:WaitForChild("KeyboardAndMouse"))
local Touch = require(script:WaitForChild("Touch"))


local lastType = Uis:GetLastInputType()
Uis.LastInputTypeChanged:Connect(function(newInputType)
	if newInputType == Enum.UserInputType.Keyboard and Uis.MouseEnabled then
		KeyboardandMouse:Enabled()
	elseif newInputType == Enum.UserInputType.Touch then
		Touch:Enabled()
	end
end)
2 Likes

It just comes down to what you want. Im not gonna read all the code but id assume the roblox one is for broad use and has alot of extra functionality you might use. If you dont need or care for it, go with your own. But the roblox devs do also know what works better about roblox so again it depends.

1 Like