How do I stop animation when player is not moving

Hello, I have a stamina script and I want to fix it when I run and I am not moving it still doesn’t do the animation unless I move. Here is my script:

Scripts
--= Variables =--

local camera = game.Workspace.CurrentCamera

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

local Bar = script.Parent:WaitForChild('STMBackground'):WaitForChild('Bar')
local player = game.Players.LocalPlayer

local debounce = true
local NormalWalkSpeed = 13
local NewWalkSpeed = 16

local power = 10
local sound = script.Parent["Heavy Breathing ( Man )"]

local sprinting = false

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

local character = player.Character

--= Tween Functions =--

-- sprinting

        local FOVChanges = {
	        FieldOfView = 90
        }
        local TweenInformation = TweenInfo.new(
	        1, --tween length
	        Enum.EasingStyle.Quint, --easing Style
	        Enum.EasingDirection.Out, --easing Direction
	        0, --repitition time
	        false, --reverse?
	        0 --delay				
        )

        local tween = TweeningService:Create(camera,TweenInformation,FOVChanges)

-- walking

        local FOVChanges2 = {
	        FieldOfView = 70
        }
        local TweenInformation2 = TweenInfo.new(
	        1, --tween length
	        Enum.EasingStyle.Quint, --easing Style
	        Enum.EasingDirection.Out, --easing Direction
	        0, --repitition time
	        false, --reverse?
	        0 --delay				
        )
 
        local tween2 = TweeningService:Create(camera,TweenInformation2,FOVChanges2)

--= Functions =--

UIS.InputBegan:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = NewWalkSpeed
		sprinting = true
		sound:Play()
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://6437730800'
		 PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		while power > 0 and sprinting do
			power = power - .05
			Bar.Size = UDim2.new(power / 10, 0, 1, 0)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
			tween:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
				tween2:Play()
				PlayAnim:Pause()
			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
		PlayAnim:Stop()
		sound:Pause()
		while power < 10 and not sprinting do
			power = power + .02
			Bar.Size = UDim2.new(power / 10, 0, 1, 0)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
			tween2:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)

Here’s a video of the problem: https://gyazo.com/75fccb00ca57d743df85ac3b28c072a0

I believe the reason why the animations persist even having stopped sprinting, is that local Anim = Instance.new('Animation') is created inside UIS.InputBegan function. Therefore, when UIS.InputEnded is triggered, it will not listen for the PlayAnim:Stop() since it’s created and localized in another function.

Try moving the animation outside the function, then :Play() or :Stop() when needed.

local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://6437730800'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)

UIS.InputBegan:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		PlayAnim:Play()
		--Rest of code
	end
end)

UIS.InputEnded:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		PlayAnim:Stop()
		--Rest of code
	end
end)
1 Like

make it so it plays when the humanoids movedirection property is not equal to 0,0,0
also put the animation variable at the start of the script so you can use stop() and play() anywhere

hope that helps