Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I check if a player is holding down a button with ContextActionService?

Asked by 5 years ago

I am able to check if a person is holding down a button with UserInputService, but I want to create a button that is available on mobile. This is the function I wrote with UserInputService (Top), and this is what I could come up with for a function that I need to use for ContextActionService (Bottom).

    UserInputService.InputBegan:Connect(function(Input, GameStuff)
if GameStuff then return end
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        isPressed = true
        LoadRunAnim:Play()
        while UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and isPressed do
            wait()
            Char.Humanoid.WalkSpeed = RunningSpeed.Value
            game.Workspace.CurrentCamera.FieldOfView = 72
        end
        isPressed = false
        game.Workspace.CurrentCamera.FieldOfView = 70
        Char.Humanoid.WalkSpeed = WalkingSpeed.Value
        end
    end)
    UserInputService.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        isPressed = false
    end
end)

function Sprinting(name, inputState, inputObject)
    if inputState ~= Enum.UserInputState.Begin then return end
    LoadRunAnim:Play()
    while UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and isPressed do
        wait()
        Char.Humanoid.WalkSpeed = RunningSpeed.Value
        game.Workspace.CurrentCamera.FieldOfView = 72
    end
    isPressed = false
    game.Workspace.CurrentCamera.FieldOfView = 70
    Char.Humanoid.WalkSpeed = WalkingSpeed.Value  
end

ContextAction:BindActionToInputTypes("Sprint",Sprinting,true)

1 answer

Log in to vote
0
Answered by 5 years ago

Nvm, I figuredit out.

Appearently you have to leave out:

if inputState ~= Enum.UserInputState.Begin then return end

to do so.

Ad

Answer this question