Play is not a valid member of Animation "Workspace.Dede_4242.Animate.SwordEquip"

I have a LocalScript under a tool, and I need the tool when being equipped to play an animation, same with unequipping, but the problem is that it throws out an error:
Play is not a valid member of Animation "Workspace.Dede_4242.Animate.SwordEquip"
the script:

--//Variables//--

local tool = script.Parent

local cooldown = 0.25

local players = game:GetService("Players")

local inUse = false
local equipped = false

local character = tool.Parent.Parent.Character or tool.Parent.Parent.CharacterAdded:Wait()

local equipAnimation = character:WaitForChild("Animate").SwordEquip
local idleAnimation = character:WaitForChild("Animate").SwordIdle
local unequipAnimation = character:WaitForChild("Animate").SwordUnequip

--//Variables//--

--//Functions//--

local function onEquip()
	print("Equipped")
	equipAnimation:Play()
	equipAnimation.Ended:Wait()
	equipped = true
end

local function onUnequip()
	print("Unequipped")
	equipped = false
	unequipAnimation:Play()
end

--//Functions//--

--//Events//--

tool.Equipped:Connect(onEquip)
tool.Unequipped:Connect(onUnequip)

--//Events//--

You are just defining the Animation and not AnimationTrack. AnimationTrack can be played but an Animation canā€™t.
Try replacing local equipAnimation = character:WaitForChild("Animate").SwordEquip with local equipAnimation = character:WaitForChild("Animate"):LoadAnimation(SwordEquip)

2 Likes

I canā€™t test this now, Iā€™ll mark it as the solution of it works

You didnā€™t load the animation with Humanoid.Animate:LoadAnimation first. Go test @uhSkittleā€™s code as it will likely work for you.

1 Like

That code still has some issues, the code that should correctly work is

local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid.Animator
local Load = animator.LoadAnimation -- You can also use the method directly :LoadAnimation

local animate = character:WaitForChild("Animate")
local equipTrack = Load(animator, animate.SwordEquip)
local idleTrack = Load(animator, animate.SwordIdle)
local unequipTrack = Load(animator, animate.SwordUnequip)
-- ...

Also, from what I can see on the method you use to get the character, this is a server script, I would recommend dealing with animations on just the client who is getting the animations, maybe a character local script, since animator instance will still replicate the animations to the other instances.

1 Like

No this is a LocalScript in a tool, I is e a server script to add the animations to the animator

Then for getting the character you should use LocalPlayer, rather than tools ancestors

You can use either type of script to get the characterā€™s animator.

No I meant I have a server script to add the animations to the animator

Try putting this at the start of your local script (Line 1)

repeat wait()

until
game:IsLoaded() == true

The issue youā€™re experiencing is likely due to the fact that the paths in your script are pointing to the Animation instances, rather than the AnimationTrack instances that you want to play. To fix this issue, you should create and load the animations using the Humanoid:LoadAnimation() function. Hereā€™s the updated script:

--//Variables//--

local tool = script.Parent

local cooldown = 0.25

local inUse = false
local equipped = false

local character = tool.Parent.Parent.Character or tool.Parent.Parent.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local equipAnimation = humanoid:LoadAnimation(character:WaitForChild("Animate").SwordEquip)
local idleAnimation = humanoid:LoadAnimation(character:WaitForChild("Animate").SwordIdle)
local unequipAnimation = humanoid:LoadAnimation(character:WaitForChild("Animate").SwordUnequip)

--//Variables//--

--//Functions//--

local function onEquip()
	print("Equipped")
	equipAnimation:Play()
	equipAnimation.Stopped:Wait()
	equipped = true
end

local function onUnequip()
	print("Unequipped")
	equipped = false
	unequipAnimation:Play()
end

--//Functions//--

--//Events//--

tool.Equipped:Connect(onEquip)
tool.Unequipped:Connect(onUnequip)

--//Events//--

In this updated script, Iā€™ve added a Humanoid reference and used Humanoid:LoadAnimation() to load the animations from the Animation instances. This function returns an AnimationTrack object, which can be played and stopped using the :Play() and :Stop() methods, respectively.

Itā€™s giving the same error

Your script doesnā€™t work neither, itā€™s giving the same error

It says:
ā€œattempt to index nil with ā€˜LoadAnimationā€™ā€

Thatā€™s because you didnā€™t wait for the animator to load.

No, itā€™s saying that LoadAnimation isnā€™t existing, and Iā€™m using WaitForChild

Are you calling it on the animator? Also, the script is wrong. Itā€™s supposed to have defined animations instead of putting them in the humanoid. local animator = Humanoid:WaitForChild("Animator") then do animator:LoadAnimation(yourAnimation).

But the animator isnā€™t a script under the Humanoid, itā€™s under the character

Edit: Iā€™m so dumb, I was indexing the animate script and not the animator

Fro some reason when I try doing:

local animator = humanoid:WaitForChild(ā€œAnimatorā€, 10)

It throws an error:
attempt to index nil with ā€˜WaitForChildā€™
(It happens also without the timeout, and with FindFirstChild)

You need to wait for the humanoid too.