Why does this animation script not work?

It should be placed inside Tool, like this:

image

i showed in the first post my hierarchyScreen Shot 2021-04-05 at 1.39.41 PM :

Oh yeah I just realized your code won’t work, because of this line:

By default, the Tool would already be inside the Player’s Backpack & not to the Character Model when they first spawn in, so that won’t work which is resulting in that “infinite yield warning”

Bingo :+1: Although might wanna do a small sanity check for defining the Character first

So it would be like this:

local Player = game:GetService("Players").LocalPlayer
local Tool = script.Parent
local Animation = Tool.Animation
local Character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")

local AnimationTrack = humanoid:WaitForChild("Animator"):LoadAnimation(Animation)

Tool.Activated:Connect(function()
    AnimationTrack:Play()
end)

(In a localscript)

1 Like

Can you try this script but with debugs?

local Tool = script.Parent
local Animation = Tool.Animation
local Character

Tool.Activated:Connect(function()
    print("Activated")
	local humanoid = Character:WaitForChild("Humanoid")
	local AnimationTrack = humanoid:LoadAnimation(Animation)
	AnimationTrack:Play()
    print("Should be playing?")
end)

Tool.Equipped:Connect(function()
    Character = Tool.Parent
    print(Character)
end)
1 Like

I get the error:
Players.nicknickphoenix.Backpack.Axe.LocalScript:4: attempt to index nil with ‘WaitForChild’

Done, I corrected the script, now it works, right?

It does print Activated, Should be playing?. Here is a video:Uploading: thisiswhathappens.mov…

The video ain’t loading mate, reload it again a

It might be because you don’t own the animation, if you own it then I don’t know what else to tell you. But if you don’t make your own animation and see if it works from there.

Sorry, here it is:

It still needs the animator before loadanimation like sotr654 said before

What do you mean by that? Is it an instance or…

Animator is a child object of Humanoid the old Humanoid:LoadAnimation(Animation) is depricated try Humanoid.Animator:LoadAnimation(Animation) Animator (roblox.com)

So I should replace

local AnimationTrack = humanoid:LoadAnimation(Animation)

with

local AnimationTrack = humanoid.Animator:LoadAnimation(Animation)

?
If so, I’ve tried that and I didn’t get any errors…

1 Like

Did you know humanoid load animation is deprecated? Use Humanoid.Animator:LoadAnimation() Deprecating LoadAnimation on Humanoid and AnimationController

Oh, I forgot to say that even though I didn’t get any errors, it still didn’t work and nothing happened.

As of now, what code do you have so far? And what type of script is it, localscript or regular script?

1 Like

local script:

local Tool = script.Parent
local Animation = Tool.Animation
local Character

Tool.Activated:Connect(function()
	print("Activated")
	local humanoid = Character:WaitForChild("Humanoid")
	local AnimationTrack = humanoid:LoadAnimation(Animation)
	AnimationTrack:Play()
	print("Should be playing?")
end)

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	print(Character)
end)

Screen Shot 2021-04-05 at 9.59.38 PM
Also, I have to go to sleep so I won’t respond for a few hours :sleeping:

Try this out?

local Tool = script.Parent
local Animation = Tool.Animation
local Character,Humanoid,Track

Tool.Activated:Connect(function()
	print("Activated")
	Track:Play()
	print("Should be playing?")
end)

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	Humanoid = Character:WaitForChild("Humanoid")
	if not Track then
		Track = Humanoid.Animator:LoadAnimation(Animation)
	end
	print(Character)
end)

Tool.Unequipped:Connect(function()
	Character = nil
	Humanoid = nil
    Track:Stop() --Optional, remove if unneeded
end)

Made it so it sets the Character and Humanoid to nil when you unequip the tool and set the Humanoid in Equipped and if the Track is not already loaded onto the Humanoid’s Animator, load it

@sudachipapa Thanks for pointing that out!

1 Like