Auto Hold Sprint instead of Holding Sprint

Hey, I made a run button. It works but I have to hold it to run. I want it so if you tap it once, it auto holds it and if u tap again, it stops running.

-- Sprint
local Humanoid = Character:WaitForChild('Humanoid')
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://11454166313'
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == 'Sprint' then
		Running = true
		Humanoid.WalkSpeed = 26
	elseif InputState == Enum.UserInputState.End and BindName == 'Sprint' then
		Running = false
		local playAnim = Character.Humanoid:LoadAnimation(rollAnim)
		if playAnim.IsPlaying then
			playAnim:Stop()
		end
		if RAnimation.IsPlaying then
			RAnimation:Stop()
		end
		Humanoid.WalkSpeed = 16
	end
end

Humanoid.Running:connect(function(Speed)
	if Speed >= 10 and Running and not RAnimation.IsPlaying then
		RAnimation:Play()
		Humanoid.WalkSpeed = 26
	elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
		RAnimation:Stop()
		Humanoid.WalkSpeed = 16
	elseif Speed < 10 and RAnimation.IsPlaying then
		RAnimation:Stop()
		Humanoid.WalkSpeed = 16
	end
end)

Humanoid.Changed:connect(function()
	if Humanoid.Jump and RAnimation.IsPlaying then
		if RAnimation.IsPlaying then
			RAnimation:Stop()
		end
		RAnimation:Stop()
	end
end)

ContextActionUtility:BindAction("Sprint",Handler,true, Enum.KeyCode.LeftShift)
ContextActionUtility:SetTitle("Sprint","Run")
1 Like

Remove the section that executes when InputState is UserInputState.End. Then, change the code in the first if statement to this:

if Running then
    Running = false
    Humanoid.WalkSpeed = 16
else
    Running = true
    Humanoid.WalkSpeed = 26
end
1 Like

Changed it and it still does the same thing for some reason.

Like this?

function Handler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == 'Sprint' then
		if Running then
			Running = false
			Humanoid.WalkSpeed = 16

			local playAnim = Character.Humanoid:LoadAnimation(rollAnim)
			if playAnim.IsPlaying then
				playAnim:Stop()
			end
			if RAnimation.IsPlaying then
				RAnimation:Stop()
			end
		else
			Running = true
			Humanoid.WalkSpeed = 26
		end
	end
end
1 Like

You’re looking for a toggle.

Pseudocode:

func key_press()
    running = NOT running
    while running:
         run()
end

Hope this helps.

You can do this by checking to see if the player is running, which is what you have in your code. If the player press shift and is running, stop running. If the player press shift and is not running, turn on running. So a pseudocode would be something like this:

if press shift then
     if running then
          stop running
     else
          start running
     end
end

I think it’s best not to have “while running” here since it’s checking too many unnecessary statements. So something like checking after every shift button pressed would reduce the number of iterations while having the script perform better.

1 Like

Good point, better to save on resources rather than constantly check!

Thanks for the correction :smile:

1 Like

I forgot to mention the button was created for mobile, only mobile users are able to use it.

Should be in the same format. Instead of shift, it would be something like “press button”.