Stamina still drains even when player is not above the running speed threshold

  1. What do you want to achieve? Stop stamina from draining when player walkspeed either becomes 0 or less than 16

  2. What is the issue? Stamina still drains even when player is not sprinting

  3. What solutions have you tried so far? i tried checking for humanoid walkspeed then stop draining stamina but it regenerates it instantly and messes with gui scaling.

The whole script is localscript.

UIS.InputEnded:connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if not (character.Humanoid.WalkSpeed <= 7) then
			character.Humanoid.WalkSpeed = 16
			sprinting = false
			RunningAnim:Stop()
			while stamina < 10 and not sprinting do
				stamina = stamina + .03
				Bar.Size = UDim2.new(stamina / 10, 0, 1, 0)
				--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
				walktween:Play()
				wait()
				if stamina <= 0 then
					walktween:Play()
					RunningAnim:Stop()
					character.Humanoid.WalkSpeed = 16
				end
				end
		end
	end
end)
local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer

local stamina = 100 -- Stamina Value
local sprinting = false
local SprintingSpeed = 30 -- The Humanoid WalkSpeed when sprinting
local MinimumStaminaToSprint = 1 -- Can change this to what you wish for the minimuman stamina to be
local StaminaIncreaseValue = 0.03 -- The value which the stamina will be increased when walking


local function RegenerateStamina() -- Function that runs when the player is walking
	if not sprinting then
		player.Character.Humanoid.WalkSpeed = 16
		repeat
			if stamina < 100 then
				stamina = stamina + StaminaIncreaseValue
				print(stamina)
				wait()
			end
		until stamina >= 100 or sprinting
	end
end

local function Sprint() -- Function that runs when the player is sprinting
	repeat
		player.Character.Humanoid.WalkSpeed = SprintingSpeed
		stamina = stamina - 1
		print(stamina)
		wait()
	until stamina < MinimumStaminaToSprint or sprinting == false
	sprinting = false
	RegenerateStamina()
	
end

UIS.InputEnded:Connect(function(input, Processed)
	if not Processed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if stamina > MinimumStaminaToSprint then 
				sprinting = true
				Sprint()
			else
				RegenerateStamina()
			end
		end
	end
end)


This is the solution I have come up with, you can paste your Animations/Tweens within those functions to make it work as normal. I’ve commented what you can change to make all of this easier. Let me know if you need further assistance with this or if you have any other questions.

1 Like