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)