Animation stop playing when moving

I’m building a shift to sprint mechanism with custom animation, but when I stop moving and hold the shift key, the sprint animation plays, and then when i stop holding the shift key the walk animation plays. How can I make check to stop this from happening?

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local runSpeed = 30
local walkSpeed = 16
local isSprinting = false

local walkAnimationId = "http://www.roblox.com/asset/?id=17113841208"
local sprintAnimationId = "http://www.roblox.com/asset/?id=17113833834"

local walkAnimTrack = nil
local sprintAnimTrack = nil

local TweenService = game:GetService('TweenService')

local Mana = 100
local SprintCost = 5
local SprintCooldown = 0.5
local lastSprintTime = 0

character.Animate.walk.WalkAnim.AnimationId = walkAnimationId

local function loadAnimations()
	local walkAnimation = Instance.new("Animation")
	walkAnimation.AnimationId = walkAnimationId
	walkAnimTrack = humanoid:LoadAnimation(walkAnimation)

	local sprintAnimation = Instance.new("Animation")
	sprintAnimation.AnimationId = sprintAnimationId
	sprintAnimTrack = humanoid:LoadAnimation(sprintAnimation)
end

local function shiftToSprint()
	if isSprinting then
		if Mana < SprintCost then
			isSprinting = false
			humanoid.WalkSpeed = walkSpeed
			if sprintAnimTrack then
				sprintAnimTrack:Stop()
			end
			if walkAnimTrack then
				walkAnimTrack:Play()
			end
			return
		end
	else
		local currentTime = tick()
		if currentTime - lastSprintTime < SprintCooldown then
			return
		end

		if Mana >= SprintCost then

			Mana = Mana - SprintCost
			print(Mana)

			humanoid.WalkSpeed = runSpeed
			TweenService:Create(camera, TweenInfo.new(0.5), {FieldOfView = 80}):Play()
			isSprinting = true
			if walkAnimTrack then
				walkAnimTrack:Stop()
			end
			if sprintAnimTrack then
				sprintAnimTrack:Play() 
			end

			lastSprintTime = currentTime
		end
	end
end

local function checkMovement()
	local lastVelocity = Vector3.new(0, 0, 0)
	while wait(0.1) do
		local currentVelocity = humanoid.RootPart.Velocity
		if currentVelocity.magnitude < 0.1 and lastVelocity.magnitude >= 0.1 then
			if walkAnimTrack and not isSprinting then
				walkAnimTrack:Stop()
			end
		end
		lastVelocity = currentVelocity
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, IS)
	if IS == true then return end
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		shiftToSprint()
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, IS)
	if IS == true then return end
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		if isSprinting then
			TweenService:Create(camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
			isSprinting = false
			humanoid.WalkSpeed = walkSpeed
			if sprintAnimTrack then
				sprintAnimTrack:Stop()
			end
			if walkAnimTrack then
				walkAnimTrack:Play() 
			end
		end
	end
end)

loadAnimations()

spawn(checkMovement)

Thank you :smiley:

Load the animation and play it directly when you start sprinting too

isnt that what im doing?

–character limit