Animation function never ends

I am trying to play an animation on a character. The animation in this code is of highest priority, but I still have other animations I want to play on the character. This animation only takes .5 seconds but after I play this, I am unable to play any other animations on the character. It appears that this function never ends because the print never prints. I don’t know if it is meant to be like that as I am quite unfamiliar with using animations.

 function playAnimation(character)

	local humanoid = character:FindFirstChildOfClass("Humanoid")
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://15180690681"
	
	if humanoid then

		-- need to use animation object for server access

		local animator = humanoid:FindFirstChildOfClass("Animator")

		if animator then

			local animationTrack = animator:LoadAnimation(animation)

			animationTrack:Play()
			
			return animationTrack
		
		end

	end
print("animation ended")
end

Either

  1. Your function isn’t being called, or
  2. Your function is returning a value which means it doesn’t get to the print statement

Here:

function playAnimation(character)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://15180690681"
	
	if humanoid then
		local animator = humanoid:FindFirstChildOfClass("Animator")
		
		if animator then
			local animationTrack = animator:LoadAnimation(animation)
			animationTrack:Play()

			animationTrack.Completed:Wait()
			
			print("Animation ended")
			
			return animationTrack
		end
	end
end

I used animationTrack.Completed:Wait() method to wait for the animation to finish playing before returning.

I figured it out. It was something not concerning this script.

1 Like