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

How do I write a mobile button that has different functions for if you are or aren't pressing it?

Asked by 4 years ago

I'm trying to convert a sprint button that I successfully made on the shift button, work on mobile. When I just tap the button, it causes me walking, whether holding down the button or not, to cause sprinting. from what I can see it doesn't detect when the button is not being pressed as it continues to go through the repeat section.

function Sprinting(actionName, inputState, inputObj)
    if SprintAvailable == true then
        if SprintAvailable and Stamina.Value > 0 and inputState == Enum.UserInputState.Begin then
                --Press to run
                print(BaseStamina.Value)
                print("Running")
                Char.Humanoid.WalkSpeed = RunningSpeed.Value
                --Start running chain
                repeat
                    if Humanoid.MoveDirection.Magnitude > 0.1 then
                        isPressed = true
                        Char.Humanoid.WalkSpeed = RunningSpeed.Value
                        game.Workspace.CurrentCamera.FieldOfView = 72
                        Stamina.Value = Stamina.Value - 1
                        Interface:Update()
                        MainFrame.BackgroundTransparency = 0
                        BarFrame.BackgroundTransparency = 0
                    end
                    wait(0.1)
                --Stop running
                until inputState == Enum.UserInputState.None or Stamina.Value <= 0
                print(inputState)
                print("Not running")
                Char.Humanoid.WalkSpeed = WalkingSpeed.Value
                game.Workspace.CurrentCamera.FieldOfView = 70
                --If stamina runs out
                --Revert sprint status
                if Stamina.Value <= 0 then
                    SprintAvailable = false
                    MainFrame.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
                    Humanoid.WalkSpeed = 1
                    wait(1)
                    Humanoid.WalkSpeed = WalkingSpeed.Value
                    MainFrame.BackgroundColor3 = Color3.fromRGB(150, 0, 0)
                    wait(5)
                    MainFrame.BackgroundColor3 = Color3.fromRGB(0, 50, 100)
                    MainFrame.BackgroundTransparency = 1
                    BarFrame.BackgroundTransparency = 1
                    SprintAvailable = true
                end
                MainFrame.BackgroundTransparency = 1
                BarFrame.BackgroundTransparency = 1
                while inputState == Enum.UserInputState.None and Stamina.Value < BaseStamina.Value do
                    print("Reverting Stamina")
                    Stamina.Value = Stamina.Value + 1
                    isPressed = false
                    Interface:Update()
                    wait(0.5)
                end
            end
        end
    print(inputState)
end

print("ended sprint")

ContextAction:BindActionToInputTypes("Sprint",Sprinting,true, Enum.KeyCode.Compose)
ContextAction:SetTitle("Sprint", "Sprint")
ContextAction:SetPosition("Sprint", UDim2.new(0.7, -25,0.1, -25))

250

Running

Enum.UserInputState.End

Updating (x18)

0
Are you saying that for mobile, when the player holds down the button, it doesn't run? B_rnz 171 — 4y
0
@B_rnz With this code, it runs, but it keeps repeating line 9 - 18. I'm trying to make it so that when the player let's go, it continues onward to the second part of the function. ninja_eaglegamer 61 — 4y

Answer this question