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.
-- 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)
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.
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.