Many people, including myself, have run into this error. I haven’t found any working solutions on the DevForum, and I spent days trying to figure out how to fix this. Today I ended up figuring it out, and I decided to post it here.
Code that would error like this:
local players = game:GetService('Players')
local animationId = 13571756345
local animation = Instance.new('Animation')
animation.AnimationId = 'rbxassetid://'.. animationId
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild('Humanoid')
local animator = humanoid:WaitForChild('Animator')
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
end)
end)
Simple fix:
Add
game:GetService('RunService').Stepped:Wait()
before you load the animation.
Example:
local players = game:GetService('Players')
local animationId = 13571756345
local animation = Instance.new('Animation')
animation.AnimationId = 'rbxassetid://'.. animationId
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild('Humanoid')
local animator = humanoid:WaitForChild('Animator')
game:GetService('RunService').Stepped:Wait()
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
end)
end)
Adding a comment here because this comes up in google when you search it.
My case was not the same as the OP, but was very similar to the YT Video. What was happening in my case was a connection that should have been disconnected by Maid never got added to Maid.
I had it setup so the StateTracker maided the connection, and almost all references to the character / character-specific connections got maided, but the StateTracker itself never got maided so none of the tracked connections got disconnected.
This was similar to the case in the YT Video in that I had a Character that had been destroyed, and animator:LoadAnimation() was being called on the Character post-destroy. So for anyone using single-script-architecture, beware, this could note a memory leak.