Stamina Won't Recharge if Player is walking?

I have a running script with stamina. Problem is, if the player is walking (not running) OR the Stamina is higher than 0, it won’t recharge. Any fixes?

--// SERVICES
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local ReplStorage = game:GetService("ReplicatedStorage")
local Event = ReplStorage.Events:WaitForChild("StaminaEvent")

--// PLAYER
local Players = game:GetService("Players")
local Player = Players.localPlayer

--// CHARACTER
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")

--// CAMERA
local Cam = game.Workspace.Camera

--// RUNNING
local Running = false
local RunKey = Enum.KeyCode.LeftShift

local Stamina = script.Stamina.Value
local Config = script.Stamina.Configuration
local MinStamina 	= Config.Min.Value
local MaxStamina = Config.Max.Value
local DrainRate = Config.DrainRate.Value
local RunRefresh = Config.RunRefresh.Value

local Run = Instance.new("Animation")
Run.AnimationId = "rbxassetid://".. 11349075080
local RunAnim = Humanoid:LoadAnimation(Run)

--// FOV
local fovup = TS:Create(Cam, TweenInfo.new(0.7), {FieldOfView = 70})
local fovdown = TS:Create(Cam, TweenInfo.new(0.5), {FieldOfView = 60})

--// IS RUNNING
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == RunKey and Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		Running = true
		Humanoid.WalkSpeed = 20
		RunAnim:Play(); fovup:Play()
	end
end)

--// STOP RUNNING
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == RunKey then
		Humanoid.WalkSpeed = 10
		RunAnim:Stop(); fovdown:Play()
		Running = false
	end
end)

Humanoid.StateChanged:connect(function(state)
	if state == Enum.HumanoidStateType.Freefall then
		RunAnim:Stop()
	elseif state == Enum.HumanoidStateType.Jumping then
		RunAnim:Stop()
	elseif state == Enum.HumanoidStateType.Landed and Running == true then
		RunAnim:Play()
	end
end)

--// RUNNING FAILSAFE
RS.Heartbeat:Connect(function(DeltaTime)
	
	
	if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		RunAnim:Stop()
		Running = false
	elseif Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and Running == true and not RunAnim.isPlaying and not Enum.HumanoidStateType.Jumping and Humanoid.WalkSpeed >= 15 then
		RunAnim:Play()
	end
	
	if Humanoid.Health < 26 then
		Humanoid.WalkSpeed = 10
		RunAnim:Stop()
		Running = false
	end
	print("Stamina: ".. math.round(Stamina))
	if Running then 
		if Stamina > MinStamina then
			Stamina = Stamina - DrainRate * DeltaTime
			Event:Fire(Stamina)
		elseif Stamina <= MinStamina then
			Humanoid.WalkSpeed = 10
			RunAnim:Stop(); fovdown:Play()
			Running = false
		end
		if not Running and Stamina < MaxStamina then 
			wait(RunRefresh)
			repeat wait(DeltaTime); Stamina = Stamina + DrainRate * DeltaTime until Stamina >= 100 or Running
		end
	end
	
	if Stamina < MinStamina then Stamina = 0 end
	if Stamina > MaxStamina then Stamina = 100 end
end)

This part of the code:

… must be part of the if Running then if statement:

if Running then 
		if Stamina > MinStamina then
			Stamina = Stamina - DrainRate * DeltaTime
			Event:Fire(Stamina)
		elseif Stamina <= MinStamina then
			Humanoid.WalkSpeed = 10
			RunAnim:Stop(); fovdown:Play()
			Running = false
		end
	elseif not Running and Stamina < MaxStamina then 
		wait(RunRefresh)
		repeat wait(DeltaTime)
		Stamina = Stamina + DrainRate * DeltaTime until Stamina >= 100 or Running
	end

If the Player stopped running, the stamina replenishing part of the code isn’t going to be activated, as the player has to be, and not be running at the same time.

oh, understood. Also, if I try Running while it’s recharging, it’ll both Discharge and Recharge at the same time, Recharging being faster. I’ve tried adding a debounce but that didn’t fix it either
I’ve also noticed my Stamina sometimes goes over 100 before going back, is there any way to cap it so it can’t go over?

RS.Heartbeat:Connect(function(DeltaTime)
	print("Stamina: ".. math.round(Stamina))
	if Running then 
		Recharging = false
		if Stamina > MinStamina then
			Recharging = false
			Stamina = Stamina - DrainRate * DeltaTime
		elseif Stamina <= MinStamina then
			Humanoid.WalkSpeed = 10
			RunAnim:Stop(); fovdown:Play()
			Running = false
		end
	elseif not Running and Stamina < MaxStamina then 
		wait(RunRefresh)
		Recharging = true
	end
	
	if Stamina < MinStamina then Stamina = 0 end
	if Stamina > MaxStamina then Recharging = false; Stamina = 100 end
	
	if Recharging then
		repeat wait(DeltaTime);
			Stamina = Stamina + DrainRate * DeltaTime until Recharging == false or Stamina == 100
	end
end)

I’m pretty sure, all these can be simplified into 3 lines.

if Recharging then
	Stamina = math.clamp(Stamina + DrainRate * DeltaTime, 0, 100)
end

Also, using any loop with a task.wait() or wait() function inside isn’t really a good idea, as RunService.Heartbeat does not wait for the function to stop, and will continue executing connected functions while the previous calls are still running.

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