Easier way of doing this?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Alright, so whenever you have a game, especially a title screen, you want to be able to detect when a player presses a button, taps the screen, or presses a button on their gamepad.
  2. What is the issue? Include screenshots / videos if possible!
    But in doing so, you would have to make 3 UserInputService functions for it to work.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried maybe looping through a table and trying to get every UserInputType.
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
-- Do the computer thing
end)

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
-- Do the mobile thing
end)

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
-- Do the gamepad thing
end)
1 Like

Use one input function and make it scan for whichever input you want inside of the actual function instead of using multiple.

1 Like
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
    if gameProcessedEvent then return end -- Ensure the event wasn't processed 

    if Input.UserInputType == Enum.UserInputType.Keyboard then
        -- Do the computer thing
    elseif Input.UserInputType == Enum.UserInputType.Touch then
        -- Do the mobile thing
    elseif Input.UserInputType == Enum.UserInputType.Gamepad1 then
        -- Do the gamepad thing
    end
end)

This should help you.

2 Likes

Do I have to make separate functions for a UserInputService.InputBegan and UserInputService.InputEnded?

Like this:

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)

end)

UserInputService.InputEnded:Connect(function(Input, gameProcessedEvent)

end)
1 Like

You can, but not necessarily. The method i sent should be the easier way.

2 Likes

Alright. I’m going to test this out and return results. Edit: It worked flawlessly. Thank you.

1 Like

You can just use Activated and then code in the gamepad logic.

1 Like

What about using the context action service?

It lets you do things like this:

--Callback for a bound action
--You can have one for every bound action or create a unique callback for every single one
local function handleAction(actionName, inputState, inputObject)
	if actionName == "TestAction" then

		if inputState == Enum.UserInputState.Begin then
			--Do something if the player begins pressing a button
		end

	end
end


--You can pass as many keys to the :BindAction() function as you want. Each key will trigger the callback
--You can also automatically create buttons for mobile players by setting the third parameter to true 
--The name of the action can be whatever you want, it can be used to unbind an action later
ContextActionService:BindAction("TestAction", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)

--For example like this:
ContextActionService:UnbindAction("TestAction")
2 Likes

I forgot about ContextActionService. I’ll start using that in my games. :+1:
Edit: I’m trying to start of my game right by organizing everything.

2 Likes

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