Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I check if a player is holding a key or not?

I've been confused.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
            print("Computer Leftshift was pressed!")
        elseif input.KeyCode == Enum.KeyCode.RightShift then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
            print("Computer RightShift Was Pressed")
            end
        end
    end
end)

1 answer

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
        if input.KeyCode == Enum.KeyCode.LeftShift then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
            print("Computer Leftshift was pressed!")
        elseif input.KeyCode == Enum.KeyCode.RightShift then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
            print("Computer RightShift Was Pressed")
    end
end)

The above code is a fixed version of your code, theres also another method of checking what key a player has held down:

UserInputService:IsKeyDown(Enum.KeyCode.KeyHere)

This can be useful for loops and other stuff

1 comment

0

You should also use UIS.InputEnded to set the walkspeed back to 16 after you release shift key

This discussion is preserved as it was. New answers, comments, and votes are closed.