Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I have a problem playing an animation in a tool, it gave me an error why?

Asked by 5 years ago
Edited 5 years ago

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??

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

Answer Edit

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.

0
I have a question, this only works for R6, is there anyways that you can make it R15 compatible? btw thank you for helping! :) RainbowBeastYT 85 — 5y
0
^ 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. User#20279 0 — 5y
0
Also I got an error saying " Players.RainbowBeastYT.Backpack.iPhone.LocalScript:18: attempt to index upvalue 'loadedanim' (a nil value)" RainbowBeastYT 85 — 5y
0
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! RainbowBeastYT 85 — 5y
View all comments (3 more)
0
check the script above, I updated RainbowBeastYT 85 — 5y
0
Checking it right now. I'll edit my answer later to show you an updated script. User#20279 0 — 5y
0
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?? RainbowBeastYT 85 — 5y
Ad

Answer this question