Sprint animation problem

Hey! I’m trying to make a sprint script, but I noticed that the animation doesn’t stop when the player is jumping, so I tried to use humanoid states to stop the animation when the player is jumping, but now it’s really buggy (sometimes animation will be stuck playing and sometimes it will not stop when the player jumps/falls). How can i fix this?

	key = string.lower(key)
	if string.byte(key) == 48 then

		if script.CanSprint.Value == true then
			local keyConnection = mouse.KeyUp:connect(function (key)
				if string.byte(key) == 48 then
					running = false
				end
			end)
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
				wait()
			end

			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
			local anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Animation)
			sprinting = true
			
			if game.Players.LocalPlayer.Character.Humanoid.MoveDirection.Magnitude > 0 then
				anim:Play()
			end

			game.Players.LocalPlayer.Character.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
				if sprinting == true then
					if game.Players.LocalPlayer.Character.Humanoid.MoveDirection.Magnitude <= 0 then
						anim:Stop()
						animPlaying = false
					else
						if animPlaying == false then
							anim:Play()
							animPlaying = true
						end
					end
				end
			end)
			
			game.Players.LocalPlayer.Character.Humanoid.StateChanged:Connect(function()
				local state = game.Players.LocalPlayer.Character.Humanoid:GetState()
				if state == Enum.HumanoidStateType.Freefall or state == Enum.HumanoidStateType.Jumping then
					animPlaying = false
					anim:Stop()
				else
					if sprinting == true then
						if game.Players.LocalPlayer.Character.Humanoid.MoveDirection.Magnitude <= 0 then
							anim:Stop()
						else
							animPlaying = true
							anim:Play()
						end
					end
				end
			end)
			
			repeat wait() until running == false
			keyConnection:disconnect()
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 12
			anim:Stop()
			sprinting = false
			animPlaying = false
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
				wait()
			end
		end

		running = true
	end
end)```
2 Likes

Don’t worry about the first line, I just forgot to paste it, also this is only part of the script.

2 Likes