I made a script where an animation will play when you activate the tool, but when I run the script it gives me an error, why?
This script is in a local script
tool = script.Parent
handle = tool:WaitForChild("Handle")
local animation = script.Parent.EquippedAnimation
tool.Equipped:Connect(function()
end)
tool.Activated:Connect(function()
animation:Play()
end)
tool.Unequipped:Connect(function()
end)
when I click play, it gives me an error saying "play is not a valid member of animation"
Can anyone help me??
Updates I made this code after someone had helped me
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local animation = script.Parent.EquippedAnimation
local animation2 = script.Parent.R15Animation
local loadedanim
tool.Equipped:Connect(function()
local humanoid = game.Players.LocalPlayer:WaitForChild("Humanoid") --Finds humanoid
local Rigtype = humanoid.RigType
if Rigtype == "R15" then
local loadedanim = humanoid:LoadAnimation(animation2) --Loads animation
else
local loadedanim = humanoid:LoadAnimation(animation) --Loads animation
end
end)
tool.Activated:Connect(function()
loadedanim:Play() -- Plays loaded animation
end)
tool.Unequipped:Connect(function()
--Other code
end)
But here is the problem, I keep getting an error saying, "Players.RainbowBeastYT.Backpack.iPhone.LocalScript:18: attempt to index upvalue 'loadedanim' (a nil value)"
Can anyone help??

7 comments
I have a question, this only works for R6, is there anyways that you can make it R15 compatible? btw thank you for helping! :)
^ That’s on the animation itself. You’ll have to make an animation in R15 if you want an animation to work in R15, unless you’re trying to say that you want this script to work both for R6 and R15.
Also I got an error saying " Players.RainbowBeastYT.Backpack.iPhone.LocalScript:18: attempt to index upvalue 'loadedanim' (a nil value)"
Yes I meant that, I want the script to work on both R6 and R15 because I already made a R15 animation call animation2 but I need the script to find out the character type and equipped/play the right animation, thank you for helping!
check the script above, I updated
Checking it right now. I'll edit my answer later to show you an updated script.
Ummm denny, it worked when the animation is played on the left hand whoch is the hand that is not hold the tool, but when I change the animation to the right hand which is the hand with the tool it does not work for some reason, can you help me??