Help with animation script

so, my animation script checks for when a certain object has been intruduced into the workspace. Problem is, when they get destroyed, the animation script errors out Infinite yield possible with JumpyZombie:WaitForChild("Humanoid"). I know the problem, just not the solution.
heres my script.

workspace.Mobs.ChildAdded:Connect(function(object)
	if object.Name == "JumpyZombie" then
		wait(5.5)
		playAnimation(object, "Jump")
	end
end)
7 Likes

Can you show what playAnimation does?

3 Likes

basically preps an animation.

local function setAnimation(object, animName)
	local hum = object:WaitForChild("Humanoid")
	local animFolder = object:WaitForChild("Animations")
	
	if hum and animFolder then
		local animObject = animFolder:WaitForChild(animName)
		
		if animObject then
			local animator = hum:FindFirstChild("Animator")
			
			local playingTracks = animator:GetPlayingAnimationTracks()
			for i, track in pairs(playingTracks) do
				if track.name == animName then
					return track
				end
			end
			
			local animTrack = animator:LoadAnimation(animObject)
			return animTrack
		end
	end
end
4 Likes

Change all of WaitForChild to FindFirstChild because you already checked if the target object exists

3 Likes

in all of the script or just the function?

3 Likes

Just the function, you only have checks of whether the object exists or not

2 Likes

it just breaks the animation script, and nothing has animations. (except everything that has an attack animation, still works.)

2 Likes

Does it print anything or errors in the output? If not then try debugging the script and print out humanoid, animation folder and the animator object, if one of them is nil then the object was created after the script execution

2 Likes

it says “animation does not exist”. its probably checking everything for that one animation. Im not sure though.

2 Likes

Which line does it say that, or it’s just nil?

2 Likes

line 33. I am printing it with warn()

Is this where it warned? Then change this line below back to WaitForChild to see whether it works or not. If it says Infinite yield possible then it doesn’t actually exist, check the animFolder

it does exist, ive checked. it still prints it.
Screenshot 2023-09-13 170551

Wait, where are the animations created, on server or client? And is the script a server or local script? It might not show for the server if it is created locally. If it’s not then I have no idea honestly.

1 Like

its a local script, and the animations are on the server.

Can you send a reproduction file that contains all of the objects related to the problem? If we continue to fix this without my knowledge of the structure it will take forever to solve (sorry if this bothers you)

sorry to bother, but how do i make one of those? ive heard of one, just dont know how to make one.

Open a new baseplate, then copy everything to the baseplate that is used by the script (in their exact location in the game, if there is something sensitive you can replace it with a new part of the same class, and you can delete the animation ids loaded). Try to include as little objects as possible that can still reproduce the error and in their correct locations used in your game
Hope I helped!

Im pretty sure i did it.
AnimTesting.rbxl (94.9 KB)

I tested this and it somehow works completely fine. I used this script, which is not very different from the one you provided:

local function setAnimation(object, animName)
	local hum = object:FindFirstChild("Humanoid")
	local animFolder = object:FindFirstChild("Animations")

	if hum and animFolder then
		local animObject = animFolder:FindFirstChild(animName)

		if animObject then
			local animator = hum:WaitForChild("Animator")

			local playingTracks = animator:GetPlayingAnimationTracks()
			for i, track in pairs(playingTracks) do
				if track.name == animName then
					return track
				end
			end

			local animTrack = animator:LoadAnimation(animObject)
			animTrack.Looped = true
			print(animTrack)
			print(animator:GetPlayingAnimationTracks())
			return animTrack
		else warn("no anim object?")
		end
	else warn("no hum?")
	end
end

local function playAnimation(object, animName)
	local anim = setAnimation(object, animName)
	print("animation retrieved")
	anim:Play()
	print(object.Humanoid.Animator:GetPlayingAnimationTracks())
end

workspace.Mobs.ChildAdded:Connect(function(object)
	print("add")
	if object.Name == "JumpyZombie" then
		while true do
			wait(5.5)
			playAnimation(object, "Jump")
			print("success")
		end
	end
end)

Here is the output for this code:

10:21:44.876  Jump  -  Server - Script:20
10:21:44.881  {}  -  Server - Script:21
10:21:44.881  animation retrieved  -  Server - Script:31
10:21:44.881    â–Ľ  {
                    [1] = Jump
                 }  -  Server - Script:33
10:21:44.882  success  -  Server - Script:42

Try checking if the animation is r15 or r6, and if the rig is anchored. There’s nothing wrong with it currently.