Hello.
I am currently working on a stamina script, and I have this glitch where if you start to run, the running animation starts, but if you stop without letting go of shift, it keeps playing. I have tried to switch up the places where the Animation plays, but I had no luck. I am unsure how to check if the player is actually running or not, after having pressed shift, and would like some help.
Here's my script:
local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild("StaminaBackground"):WaitForChild("Stamina")
local Player = game:GetService("Players").LocalPlayer
local NormalWalkSpeed = 13
local NewWalkSpeed = 23
local Power = 10
local IsWalking = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
repeat wait() until Player.Character
repeat wait() until Player.Character.Humanoid
local Character = Player.Character
local Humanoid = Player.Character.Humanoid
local Animation = Humanoid.Animator:LoadAnimation(script.Run)
Player.Character.Humanoid.Running:Connect(function(Speed)
IsWalking = (Speed ~= 0)
end)
UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
if GameProcessedEvent then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
if not (IsWalking) then
return;
end
Character.Humanoid.WalkSpeed = NewWalkSpeed
IsSprinting = true
Animation:Play()
while Power > 0 and IsSprinting do
Power = Power - 0.03
Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
wait()
if Power <= 0 then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
Animation:Stop()
end
end
end
end)
UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
if GameProcessedEvent then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
IsSprinting = false
Animation:Stop()
while Power < 10 and not IsSprinting do
Power = Power + 0.03
Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
wait()
if Power <= 0 then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
Animation:Stop()
end
end
end
end)
If I could get some help with this, it would be great. Thanks!

1 comment
This worked very well. Thanks!