How do I manage animations now after the deprecation of Humanoid:LoadAnimation()?

I understand that now I’ll have to use animator as opposed to humanoid, and according to the devhub, it has to be created serverside and be a descendant of a humanoid or animationcontroller then accessed through a local script. My question is what I should parent the the animator to on the server script? And whether I have to fireclient() from the server to be able to access it on the local script after its creation serverside.

Also according to the devhub:“Both Humanoid:LoadAnimation() and AnimationController:LoadAnimation() will create an Animator if one does not already exist”
And this kinda condradicts what i’ve read. Do I have to instance.new it on server or not? Please clear it up for me, thanks.

2 Likes

Just stick it inside the player’s humanoid:

-- Server:
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid", 10)
		if (not humanoid) then return end
		local animator = Instance.new("Animator")
		animator.Parent = humanoid
	end)
end)

And then the server or client can use that animator to load animations. The old-school Humanoid:LoadAnimation() and AnimationController:LoadAnimation() is being deprecated because it creates a sort of race condition where the server and client might make their own animators. By explicitly creating the animator and only using that one, you remove the race condition.

If using the animator from a client, remember to use WaitForChild:

-- Client:
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid", 10)
	if (not humanoid) then return end
	local animator = humanoid:WaitForChild("Animator", 10)
	if (not animator) then return end
	-- Use animator:LoadAnimation(id) here
end)

See this thread: Deprecating LoadAnimation on Humanoid and AnimationController - Updates / Announcements - Roblox Developer Forum


Regarding the if (not _x_) then return end statements: If you’re ever using a method that might return nil (such as WaitForChild with a timeout), it is imperative that you check the variable for nil.

3 Likes

Humanoid:LoadAnimation() still works I believe, but they highly discourage the use of it anymore

Shouldn’t it be used without the paranthesis? Like if not Humanoid then.

Why do you have 10 as the second argument?

Yeah I’m aware, just trying to stay up to date with the new stuff.

1 Like

Parentheses are optional. I use them because it’s a habit from other languages I use daily. No need for them. Regarding the 10: WaitForChild accepts a second argument, which is a timeout. If it waits longer than that amount of seconds, it will stop and return nil. IMO it should be a required property, because you otherwise risk the possibility of an infinite yield.

2 Likes

I see, thank you both for the great explanations.

The thread I linked at the bottom of my post there: Deprecating LoadAnimation on Humanoid and AnimationController