Med Written By caiomgt

Controller Input Created on: 20-05-2020

How to get and do stuff with controller input!

>Before you start, this tutorial assumes you understand basic lua, events and a little bit about the UserInputService(tho not necessary it will make it easier to understand)

Dont ask for script locations, since this tutorial is not meant to be copied and

pasted.

Hello! Welcome, today you're going to learn controller input! If you dont know the UserInputService, im going to give a brief overview on what we will need here. Of course, since we're detecting input from the local player, we're gonna need a localscript. When detecting gamepad events with UserInputService, all of the controls will fire the InputBegan and InputEnded events. Both has two parameters: >Input = The input from the player. Its type is an InputObject(for more info on InputObjects, go to this link: https://developer.roblox.com/en-us/api-reference/class/InputObject >GameProcessed = Tells whether or not the player input has been processed by another script(including stuff like the chat). Its type is a boolean.

So, by their names, you can guess that InputBegan fires when The player presses a button or key down, and InputEnded fires when the player releases a button or key.

We're also gonna be using two other events from the service, being GamepadConnected and GamepadDisconnected, both have one parameter: >Gamepad = Its the gamepad connected to the game. Its type is a UserInputType enum.

As you can guess, again, GamepadConnected will fire when a Gamepad gets connected to the player's computer, and GamepadDisconnected when it disconnects You may ask yourself why do we want to use GamepadConnected and GamepadDisconnected events?

Well, we want to because we may want to track how many gamepads are connected if your game will support local multiplayer and because we dont want to waste time checking if our input in InputBegan and or InputEnded was from a gamepad when there's none connected. So first, we'll make a few starting variables:

UIS = game:GetService("UserInputService")
gamepadAvailable = false --This variable will keep track of if there
--is a gamepad connected.
gamepadNumber = 0 --This will keep track of how many gamepads there 
--are available.

Now: We'll set up the GamepadConnected and GamepadDisconnected to change the variables above and print something.

UIS.GamepadConnected:Connect(function()
	gamepadAvailable = true
	gamepadNumber = gamepadNumber + 1
	print("A gamepad has connected.")
end)
UIS.GamepadDisconnected:Connect(function()
	gamepadNumber = gamepadNumber - 1
	if gamepadNumber <= 0 then
		gamepadAvailable = false
	end
	print("A gamepad has disconnected.")
end)

Now to detect actual input, we'll use the InputBegan(and, if you may need it, InputEnded) events. We will set up the InputBegan event:

UIS.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if gamepadAvailable then
			--Detect input here
		end
	else
		return nil
	end
end)

So, first, we will check if the input wasn't already processed by another script like lets say the chat, if it was, we return nil, making it not run the rest of the code. Next, we check if there is a gamepadAvailable to detect input to start with, as InputBegan also detects any other types of input, not only from a controller, then, we will check for input if there is a gamepad. Now, im not going to use the gamepadNumber variable as im not going to do something for a local multiplayer game, but you could just check which gamepad triggered it by doing something like

if input.UserInputType == Enum.UserInputType.Gamepad1 then

Anyways, we'll detect actual input now! For my script i will check if the player pressed the A button (or green) and then print something.

if input.KeyCode == Enum.KeyCode.ButtonA then
	print("The A button has been pressed.")
end

Now, we detected button input, but you may ask:

How to detect analog sticks?

Well, its kinda different, and you'll need to use a different event called InputChanged, dont ask me why, its how it works. Now, using this, you can also detect input from the triggers, since they're also analog inputs, but, lets proceed. First, we'll setup the InputChanged event and check for the gamepads:

UIS.InputChanged:Connect(function(input)
	if gamepadAvailable then
		--We will detect it here
	end
end)

Now that we have set up the InputChanged event, we will detect both the analog sticks and the triggers. Before we do so, i have to clarify one thing: Since both are analog inputs, they have a value to specify in what position they are, the thumbsticks values are in between -1 to 1, on the X and Y axis, and the trigger being from 0 to 1 on the Z axis(0 at the starting position, and 1 when fully pressed). Now, detecting which is the same as with the buttons above, being:

if input.KeyCode == Enum.KeyCode.ThumbStick1 then --As an example, 
--im detecting the first thumbstick, but you could do any

But to get their position you have to do something different. So, the input object has a property called Position: as you may have guessed it gives you the position. But of what? Remember those values for the analog inputs i told you above? Thats where you'll be able to get them. So, for example, we could check in the X axis for input on the analog stick going left and right, like this:

print(input.Position.X) --Im only printing as an example, but you 
--could do all sorts of stuff.

You could do the same for the Y and Z axis.

And thats it! You have completed my tutorial on controller input! As a challenge, i will ask you to make if you press the blue button (or X) on your controler, for a part to show up, and if your analog stick is all the way to the right, to make it go up. If you got lost, here's all the code we made up to now.

UIS = game:GetService("UserInputService")
gamepadAvailable = false
gamepadNumber = 0
UIS.GamepadConnected:Connect(function()
    gamepadAvailable = true
    gamepadNumber = gamepadNumber + 1
    print("A gamepad has connected.")
end)
UIS.GamepadDisconnected:Connect(function()
    gamepadNumber = gamepadNumber - 1
    if gamepadNumber <= 0 then
        gamepadAvailable = false
    end
    print("A gamepad has disconnected.")
end)
UIS.InputBegan:Connect(function(input, gameProcessed)
    if not gameProcessed then
        if gamepadAvailable then
         	if input.KeyCode == Enum.KeyCode.ButtonA then
				print("The A button has been pressed.")
			end
        end
    else
        return nil
    end
end)
UIS.InputChanged:Connect(function(input)
    if gamepadAvailable then
        if input.KeyCode == Enum.KeyCode.ThumbStick1 then
			print(input.Position.X)
		end
    end
end)

Have a fun coding session with your new knowledge!

Med Written By caiomgt

See caiomgt's profile on Roblox

Discussion