Controller Input

How to get and do stuff with controller input!

by caiomgt

Author Avatar

image|400x200

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

Welcome, today you're going to learn controller input! If you dont know the UserInputService, I'm going to give a brief overview on what we will need here. Of course, since the goal is to detect player input, we need a LocalScript When detecting gamepad events with UserInputService, all of the controls will fire the InputBegan and InputEnded events. Both have 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 and InputEnded fires when the player releases a button or key.

We're also going to be using two other events from the service, being GamepadConnected and GamepadDisconnected, both have one parameter:

Gamepad = Its the gamepad connected / disconnected 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 supports 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 check if the input wasn't already processed by another script like lets say the chat, if it was, then return nil. Next, a check for if there is a gamepadAvailable to detect input, 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, to detect input: For my script i will check if the player pressed the A button (on xbox controllers) 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?

You'll need to use a different event called InputChanged, don't ask me why, it's how it works. Now, using this, you can also detect input from the triggers, since they're also analog inputs along with analog sticks. 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 others

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!

View in-game to comment, award, and more!