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)
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.
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.
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).