Getting Error "LoadAnimation requires the Humanoid object to be a descendant of the game object"

Hello, I,m making game that makes other player flying around, but something is happen when I resetting the character. It will showing the error If try to reset it.

image

Here’s the code:

    local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://4234108826"
local PlayAnim = Humanoid:LoadAnimation(Anim)
local HumaoidRP = Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()

As you can see, I wait for the humanoid to exist, I still get the error. It only seems to happen in Studio, but it’s really hindering when you want to test.

This is because the player’s character is not immediately parented to the workspace. You need to wait until the character is in the workspace using something like AncestryChanged or ChildAdded.

3 Likes

try to use:
local Character = Player:WaitForChild("Character")

That won’t work as Character is a property of Player, not an object under Player.
You would have to use something like this to achieve the same functionality

repeat wait() until Player.Character
local Character = Player.Character

Also, if you are getting this error whenever you reset, it might be that the humanoid’s parent is set to nil. If that’s the case, this is perfectly normal.

EDIT:

Yea, Player.CharacterAdded:Wait() is better

2 Likes

Instead of a repeat loop, just use local character = player.Character or player.CharacterAdded:Wait(). Even if the Character doesn’t exist, it’ll just wait until it does.

3 Likes

yes this script is way better i test it thanks man :grinning:

1 Like

As another person here said, it would be better to use local char = player.Character or player.CharacterAdded:Wait(), because it will wait until the character loads in, and will continue the script once the character loads in

3 Likes

This is a better method:

if not Player.Character then
  Player.CharacterAdded:Wait()
end
local Character = Player.Character
1 Like

I know its late, but

1 Like

69.5 years later and I know the Root solution to this problem.

Get the “Animate” script that is automatically placed in your character when you Play the game.

Paste this in Animate script after local Humanoid →
“repeat wait() until Humanoid:IsDescendantOf(game)”

4 Likes