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??
See the edited answer below the original answer
So you can’t simply play the animation just like that. You have to actually load it and then play the loaded animation.
Here’s what your script should look like :
local tool = script.Parent local handle = tool:WaitForChild("Handle") local animation = script.Parent.EquippedAnimation local loadedanim tool.Equipped:Connect(function() local humanoid = script.Parent.Parent:FindFirstChild('Humanoid') --Finds humanoid if humanoid then 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)
So what this does is it tries to find a humanoid in whatever character equips it. If it finds one, it’ll load the animation and return something called an AnimationTrack
, which is where you get to use the :Play() function. It gets saved as a variable and when the tool is activated, the animation will play.
If you have any questions, just comment!
Okay, so I checked your new script and well, the way you edited it pretty much broke the whole thing. Here's the fixed version :
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 = script.Parent.Parent:FindFirstChild('Humanoid') --LocalPlayer will not work in server scripts. Also add more parents if this script is not directly parented to the tool. if humanoid then if humanoid.RigType == Enum.HumanoidRigType.R15 then --Use enums instead of strings loadedanim = humanoid:LoadAnimation(animation2) --Putting "local" before the variable seperates it from the actual variable. Don't use local in this part. else loadedanim = humanoid:LoadAnimation(animation) --Don't use local in this one too. end end end) tool.Activated:Connect(function() loadedanim:Play() end) tool.Unequipped:Connect(function() --Other code end)
If you have any more questions, just comment. Be careful when editing this too.