I am trying to make a sprint script with a custom animation but there is a problem. The animation will still play if you aren't moving and you are holding shift. I looked at devforums for people with the same problem as me but i cant fix it.

2 scripts that i tried:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')

local VectorZero = Vector3.new(0, 0, 0)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 64
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://6011162399'
		RunAnim = Humanoid:LoadAnimation(Anim)
		if Humanoid.MoveDirection ~= VectorZero then
			RunAnim:Play()
		else
			RunAnim:Stop()
		end
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 16
		RunAnim:Stop()
	end
end)
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 64
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://6011162399'
		RunAnim = Humanoid:LoadAnimation(Anim)
		if Humanoid.MoveDirection.Magnitude > 0 then
			RunAnim:Play()
		else
			RunAnim:Stop()
		end
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 16
		RunAnim:Stop()
	end
end)