Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is the animation saying: Animation must be a descendant of a game object?

I was creating some scripts, you know...

..Dev stuff.

And I came across this error that said:

":LoadAnimation requires the Humanoid object (Sensei_Developer.Humanoid) to be a descendant of the game object"

Here's my script, BTW.

local player = game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character.Humanoid
local HRP = character.HumanoidRootPart

local Anim = Instance.new("Animation",script.Parent)
Anim.Name = "ChopAnim"
Anim.AnimationId = 'rbxassetid://3027147369'

local Animation = Humanoid:LoadAnimation(Anim)

local Part = script.Parent

game.ReplicatedStorage.PressedE.OnServerEvent:Connect(function()
	if (HRP.Position - Part.Position).magnitude < 4.5 then
		Animation:Play()
	end
end)

No idea why this is happening. Can someone tell me why this is happening, so I can learn from this? Thanks!

3 comments

0

I never actually came across this issue, try rewriting the whole script

0

Not the best advice.

0

Uh, okay.

1 answer

✓ Accepted answer

First of all you likely want to wait for the humanoid to exist first before using it. You can do this by replacing line 3 with this:

local Humanoid = character:WaitForChild("Humanoid")

Next problem is likely that the parent of character is probably nil since you just started the game. In order to combat this you can wait for the ancestry of it to change. You can do this by adding this segment of code BEFORE you wait for the humanoid:

while not character.Parent do
	character.Parent.AncestryChanged:wait()
end

what this does is it waits for the ancestry of the character's parent to be changed while the parent of the character is nil.

It's also a good practice to wait for objects before referencing them so I'd do the same :WaitForChild("HumanoidRootPart") function for the HRP. All in all the first section of code should look like this:

local player = game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
while not character.Parent do
	character.AncestryChanged:wait()
end
local Humanoid = character:WaitForChild("Humanoid")
local HRP = character:WaitForChild("HumanoidRootPart")

Hope this helped and if it did make sure to mark the answer as answered. Have a nice day!

4 comments

0

Uh, it says "Attempt to index field 'Parent' (A nil value)". -visible confusion-

0

Line 4

0

My bad, change line 4 to character.AncestryChanged:wait()

0

Ty!

This discussion is preserved as it was. New answers, comments, and votes are closed.