Disabling Sprinting While Standing Still

Hello! I just created this sprinting script and I was wondering about how I could disable the sprint meter from going down while standing still. Any ideas? Here is my script.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local staminaText = script.Parent:WaitForChild("StaminaAmount")

local UserInputService = game:GetService("UserInputService")

local stamina = 100
local running = false

function StaminaChanged()
	script.Parent.Overlay:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "In", "Sine", 0)
	staminaText.Text = stamina
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		
		while stamina > 0 and running do
			character.Humanoid.WalkSpeed = 32
			stamina = stamina - 1
			
			StaminaChanged()
			
			wait()
		end
		
		character.Humanoid.WalkSpeed = 16
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = false
		character.Humanoid.WalkSpeed = 16
		
		while stamina < 100 and not running do
			stamina = stamina + 1
			
			StaminaChanged()

			wait()
		end
	end
end)

Well you can check the humanoid movedirection and if it is equal to zero, have like a variable that tracks when it is at zero and not then put that into you checks for decreasing the stamina

Thanks, I actually found your other post looking for answers.
I tried doing this,

if humanoid.MoveDirection.Magnitude > 0 then

running = false

end

but it didn’t seem to do anything.

instead where you decreasing the speed do

if running then
 stamina = stamina - 1
end

also im not realy sure if having while loops in an inputbegan and inputended are good practice… im just realizing

I couldn’t get this to work, so instead of,
while stamina > 0 and running do
I tried,
while stamina > 0 and running and humanoid.MoveDirection.Magnitude ~= 0 do

Thank you for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.