Sprint animation keeps on playing even after I stop walking

I need help, after I stop pressing W, the animation keeps playing.

-- local sprintSpeed = 30

local sprintFov = 80
local normalFov = 70

local sprintKey = Enum.KeyCode.LeftShift

local userInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")

local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")

local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local defaultSpeed = humanoid.WalkSpeed

local camera = workspace.CurrentCamera

local sprintAnim = script:WaitForChild("SprintAnim")
local animTrack = humanoid:LoadAnimation(sprintAnim)

-- STATES
local sprinting = false

local function tweenFov(duration, fov)
	
	local tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), {FieldOfView = fov})
	tween:Play()
	
	spawn(function()
		tween.Completed:Wait()
		tween:Destroy()
	end)
	
end

userInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == sprintKey and not processed then
		if not sprinting then
			sprinting = true
			
			defaultSpeed = humanoid.WalkSpeed
			
			
			if humanoid.MoveDirection.Magnitude > 0 then
				tweenFov(0.2, sprintFov)
				if not animTrack.IsPlaying then
					animTrack:Play()
				end
				humanoid.WalkSpeed = sprintSpeed
			end
			
		end
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == sprintKey then
		sprinting = false
		
		if animTrack.IsPlaying then
			animTrack:Stop()
		end
		
		humanoid.WalkSpeed = defaultSpeed
		
		tweenFov(0.2, normalFov)
	end
end)

(post deleted by author, i’m dumb)

You could use RunService to check if the player’s MoveDirection is 0 and the if the animation is still playing stop playing. example

game:GetService("RunService").Heartbeat:Connect(function()
	if humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		
	else
		sprinting= false
		-- whatever else you gotta do
	end
end)
1 Like