Animation keeps playing after stamina depletes

Hey, I’m having an issue with animations where it keeps playing after the stamina depletes


How would I get it to stop playing the running animation once the stamina depletes and return the just the walking animation?

Just check whenever the stamina reaches 0, then stop the running animation?

If you don’t provide code, we can’t help you any further than that

Here is the localscript I use for the shift to run w/ animation

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character


UIS.InputBegan:connect(function(input) --When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
 if input.KeyCode == Enum.KeyCode.LeftShift then
  Character.Humanoid.WalkSpeed = 32
  local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://10011217576'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
  PlayAnim:Play()
 end
end)


UIS.InputEnded:connect(function(input)
 if input.KeyCode == Enum.KeyCode.LeftShift then
  Character.Humanoid.WalkSpeed = 12
  PlayAnim:Stop()
 end
end)

Here is the localscript which I use for the stamina bar

local UIS = game:GetService('UserInputService')



local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')



local player = game.Players.LocalPlayer



local NormalWalkSpeed = 12

local NewWalkSpeed = 28



local power = 10



local sprinting = false



repeat wait() until game.Players.LocalPlayer.Character



local character = player.Character



UIS.InputBegan:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then

		character.Humanoid.WalkSpeed = NewWalkSpeed

		sprinting = true

		while power > 0 and sprinting do

			power = power - .1

			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)

			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)

			wait()

			if power <= 0 then

				character.Humanoid.WalkSpeed = NormalWalkSpeed

			end

		end

	end

end)



UIS.InputEnded:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then

		character.Humanoid.WalkSpeed = NormalWalkSpeed

		sprinting = false

		while power < 10 and not sprinting do

			power = power + .03

			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)

			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)

			wait()

			if power <= 0 then

				character.Humanoid.WalkSpeed = NormalWalkSpeed

			end

		end

	end

end)

You’re gonna need a way to share the value of the stamina between those two scripts OR make the same script that handles the stamina also handle the animation.

The problem currently is that the script that runs the animation has no way of telling if you’re out of stamina, you can solve this by making an attribute or intvalue (or a module) inside something that represents the amount of stamina you have left. Then you can bind an function to a GetAttributeChangedSignal/PropertyChangedSignal and if the remaining stamina is 0, then you should stop the animation.

Alternatively, you can run the animation in the script that handles the stamina. If the player is sprinting AND has stamina, then play the animation; otherwise stop the animation if any of those conditions are false

1 Like