I made a stamina script that handles running. The stamina refills when the player stops moving. Problem is when the stamina becomes 0, it refills but it yields the entire code and the player is stuck in sprint mode because once you press sprint you don’t have to hold sprint.
wait(1.25);
local UIS = game:GetService("UserInputService")
local Tween = game:GetService("TweenService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UI = script.Parent
local Frame = UI.Frame
local Bar = Frame.Bar
local ContextActionUtility = require(game:GetService("ReplicatedStorage").Modules.ContextActionUtility)
local MaxStamina = 100
local StaminaAmount = script.STAmount
local moving = script.Moving
local allowed = script.Allowed
local RunningValue = script.Running
StaminaAmount.Value = MaxStamina
local Humanoid = Character:WaitForChild('Humanoid')
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://11793838346'
RAnimation = Humanoid.Animator:LoadAnimation(RunAnimation)
local animPlaying
Running = false
local function CheckAlive()
local Hum = Character:FindFirstChild("Humanoid")
local Head = Character:FindFirstChild("Head")
local HumanRoot = Character:FindFirstChild("HumanoidRootPart")
if Hum and Hum.Health > 0 and Head and HumanRoot then
return true
else
return false
end
end
function Handler(BindName, InputState)
if InputState == Enum.UserInputState.Begin and BindName == 'Sprint' then
if Running then
Running = false
Humanoid.WalkSpeed = 16
ContextActionUtility:SetTitle("Sprint","Run")
RunningValue.Value = false
elseif StaminaAmount.Value ~= 0 and not Running and allowed.Value == true then
ContextActionUtility:SetTitle("Sprint","STOP")
Running = true
RunningValue.Value = true
Humanoid.WalkSpeed = 20
end
end
end
Humanoid.Running:connect(function(Speed)
if Speed >= 10 and Running and not RAnimation.IsPlaying and StaminaAmount.Value ~= 0 then
RAnimation:Play()
animPlaying = true
Humanoid.WalkSpeed = 20
elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
RAnimation:Stop()
animPlaying = false
Humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
elseif Speed < 10 and RAnimation.IsPlaying then
RAnimation:Stop()
animPlaying = false
Humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
end
end)
Humanoid.Changed:connect(function()
if Humanoid.Jump and RAnimation.IsPlaying then
if RAnimation.IsPlaying then
RAnimation:Stop()
end
RAnimation:Stop()
end
end)
-- Stamina Handler
RunningValue:GetPropertyChangedSignal("Value"):Connect(function()
if RunningValue.Value == true then
while RunningValue.Value == true do
if moving.Value == true then
StaminaAmount.Value = StaminaAmount.Value - 10
print(StaminaAmount.Value)
if StaminaAmount.Value == 0 then
Tween:Create(Bar.TextLabel,TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.Out),{TextTransparency = 0}):Play()
break
end
wait(0.3)
elseif moving.Value == false then
repeat wait() until moving.Value == true
end
end
end
end)
-- Handling the tweens.
StaminaAmount:GetPropertyChangedSignal("Value"):Connect(function()
local size = StaminaAmount.Value / MaxStamina
Bar:TweenSize(UDim2.new(size,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,0.3)
if StaminaAmount.Value == 10 then
Tween:Create(Bar.TextLabel,TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.Out),{TextTransparency = 1}):Play()
end
end)
-- Checking if player is moving
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.Magnitude == 0 and StaminaAmount ~= 100 then
moving.Value = false
end
if Humanoid.MoveDirection.Magnitude ~= 0 and StaminaAmount ~= 100 then
moving.Value = true
end
end)
-- Giving Stamina if player isn't moving.
moving:GetPropertyChangedSignal("Value"):Connect(function()
if moving.Value == false and StaminaAmount.Value ~= 100 then
repeat
StaminaAmount.Value = StaminaAmount.Value + 10
wait(1.5)
until moving.Value == true
end
end)
ContextActionUtility:BindAction("Sprint",Handler,true, Enum.KeyCode.LeftShift)
ContextActionUtility:SetTitle("Sprint","Run")