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 6 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).

01    UserInputService.InputBegan:Connect(function(Input, GameStuff)
02if GameStuff then return end
03    if Input.KeyCode == Enum.KeyCode.LeftShift then
04        isPressed = true
05        LoadRunAnim:Play()
06        while UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and isPressed do
07            wait()
08            Char.Humanoid.WalkSpeed = RunningSpeed.Value
09            game.Workspace.CurrentCamera.FieldOfView = 72
10        end
11        isPressed = false
12        game.Workspace.CurrentCamera.FieldOfView = 70
13        Char.Humanoid.WalkSpeed = WalkingSpeed.Value
14        end
15    end)
16    UserInputService.InputEnded:Connect(function(Input)
17    if Input.KeyCode == Enum.KeyCode.LeftShift then
18        isPressed = false
19    end
20end)
21 
22function Sprinting(name, inputState, inputObject)
23    if inputState ~= Enum.UserInputState.Begin then return end
24    LoadRunAnim:Play()
25    while UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and isPressed do
26        wait()
27        Char.Humanoid.WalkSpeed = RunningSpeed.Value
28        game.Workspace.CurrentCamera.FieldOfView = 72
29    end
30    isPressed = false
31    game.Workspace.CurrentCamera.FieldOfView = 70
32    Char.Humanoid.WalkSpeed = WalkingSpeed.Value 
33end
34 
35ContextAction:BindActionToInputTypes("Sprint",Sprinting,true)

1 answer

Log in to vote
0
Answered by 6 years ago

Nvm, I figuredit out.

Appearently you have to leave out:

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

to do so.

Ad

Answer this question