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

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

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

✓ Accepted answer

Nvm, I figuredit out.

Appearently you have to leave out:

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

to do so.

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