Is there a way to reliably detect when a jump key on any device is pressed (like with inputBegan)?
Detection should not need to be debounced
Detection should work when the character is in the air
game:GetService("UserInputService").InputBegan:Connect(function(inputObject)
-- check if inputObject triggers a jump?
end)
What I have tried so far:
I know that you can use JumpRequest, but it fires multiple times when one button is pressed. Since I need to recognise key presses that happen in a short period of time, debouncing makes it impossible to recognise them.
There is also the option to use humanoid.Jumping, but that only works when the humanoid is on the ground, but in my case I also need to detect jump button presses in the air.
Humanoid.StateChanged:Connect(function(old, new)
-- contains the old and new StateType
if new == Enum.HumanoidStateType.Jumping then -- if HumanoidState is Jumped
print("Jumped"); -- fire jumped
end
end)
Jumping fires when the player enters and leaves the state as mentioned in its description. :GetPropertyChangedSignal("Jump") will change from true to false everytime you jump, firing 2 times for each change, hence “Get Property Change Signal” StateChanged can be explicitly used to check if the new state they are in is the Jumping state. This State wont change until the Player is in Freefall or back on the ground, but because its searching for the jumping state, it wont fire for other states unless told to do so.
Thank you for the quick reply, it works perfectly when the character is grounded, but I sadly also need to be able to detect a jump input when he is in the air (falling).
This sadly needs to be debounced (or it would trigger multiple times per button click - even with “if jump == true”), which makes it impossible to detect key presses that happen in a short period of time.
local UserInputService = game:GetService("UserInputService")
local Player: Player = game.Players.LocalPlayer
-- KeyCode (Desktop, Console)
UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
if not gameProccesedEvent and (input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.ButtonA or input.KeyCode == Enum.KeyCode.ButtonX) then
-- Do something
end
end)
-- Touch (Phone, Tablet)
if UserInputService.TouchEnabled then
local JumpButton: ImageButton = Player.PlayerGui:WaitForChild("TouchGui"):WaitForChild("TouchControlFrame"):WaitForChild("JumpButton")
JumpButton.MouseButton1Click:Connect(function()
-- Do something
end)
end