This function returns an array of
InputObject|InputObjects associated with the keys currently being pressed down.
This array can be iterated through to determine which keys are currently being pressed, using the
To check if a specific key is being pressed, use
UserInputService is client-side only, this function can only be used in a
An array of
Double Jump Key Combo
This example demonstrates how to use the
UserInputService/GetKeysPressed function to create a combo action where the player double jumps when the the player presses actionKey key (Left Shift) + Jump key (Spacebar).
The actionKey variable indicates which key, combined with the Jump key, needs to be pressed for the player to double jump.
When the player presses the Jump key, the
UserInputService/JumpRequest|JumpRequest event is invoked, which is connected to the script’s jumpRequest function. If the Left Shift key is pressed and the player is not already in the middle of a jump, this function sets the canDoubleJump boolean to true.
The example connects the stateChanged function to the
Humanoid/StateChanged|StateChanged event so that the function fires when their
Enum/HumanoidStateType|humanoid’s state changes. If the state changes from Jumping to Freefall, and the canDoubleJump boolean is true, the function makes the player jump again by setting their humanoid’s state back to Jumping using the
The example also uses the canJump boolean variable to determine when the player is in the middle of a jump. Without this variable, the player could press the actionKey + Jump Key (spacebar) to jump endlessly without landing. When the boolean is true, the player is not jumping. If the player is not jumping, jumpRequest() checks if the actionKey is pressed and sets canJump to false. When the player lands, stateChanged() sets the variable to true.
local UserInputService = game:GetService("UserInputService") local Players = game.Players local player = Players.LocalPlayer local character = player.Character local humanoid = character:WaitForChild("Humanoid") local actionKey = Enum.KeyCode.LeftShift local canJump = true local canDoubleJump = false local function jumpRequest() local keysPressed = UserInputService:GetKeysPressed() for _, key in ipairs(keysPressed) do if (key.KeyCode == actionKey and canJump) then canJump = false canDoubleJump = true end end end local function stateChanged(oldState, newState) -- Double jump during freefall if able to if (oldState == Enum.HumanoidStateType.Jumping and newState == Enum.HumanoidStateType.Freefall and canDoubleJump) then canDoubleJump = false wait(.2) humanoid:ChangeState(Enum.HumanoidStateType.Jumping) end -- Allow player to jump again after they land if (oldState == Enum.HumanoidStateType.Freefall and newState == Enum.HumanoidStateType.Landed) then canJump = true end end UserInputService.JumpRequest:Connect(jumpRequest) humanoid.StateChanged:Connect(stateChanged)