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
.