Help me with LoadAnimation

I’m making a script that triggers an animation whenever a tool is activated, here’s the script:

local players = game:GetService("Players").LocalPlayer
local character = players.Character
local animation = script.Parent:WaitForChild("Animation")
local loadAnim = character.Humanoid:LoadAnimation(animation)

script.Parent.Activated:Connect(function()
	loadAnim:Play()
end)

Can someone point the error in my script please?

According to dev hub it says that Humanoid:LoadAnimation is deprecated. I don’t know another alternative to :LoadAnimation though

Maybe that is the error.

1 Like

You said “script.Parent.Activated”, referring to the character and not any actual part.

script.Parent.Activated is the tool

OH. If it is a tool, you should be using “Equipped” not “Activated”.

Equipped is when the player starts holding the tool. Activated is when the player clicks on the screen while holding the tool. I don’t think using Equipped instead of Activated will solve the problem.

In fact, it didn’t. :frowning:

Try to add a WaitForChild(“Humanoid”)

Then idk. I haven’t played around with tools in a while.

as Dolhin_Worm said i think the problem is that

I don’t use it a lot too, but i’m really in need of it right now and all tutorials are outdated

This code when placed inside a LocalScript in Tool

local tool = script.Parent

tool.Activated:Connect(function()
	local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
	local animation = script.Parent:WaitForChild("Animation")
    local loadAnim = humanoid:LoadAnimation(animation)
	loadAnim:Play()
end)

You probably need to disable “RequiresHandle” in order for this to work.
Otherwise, you need an object named “Handle” in your tool for the script to recognize if your tool is eligible.
image

I do have a Handle in my tool, i don’t think that’s the problem

That’s basically my current script, just different

I think the problem is that the script runs before the character and humanoid are created.

You should change the character and loadAnim variable to

local character = players.CharacterAdded:Wait() or players.Character
local loadAnim = character:WaitForChild("Humanoid"):LoadAnimation(animation)

Check up

The error is not in the code

I checked the text I posted in Studio and it works fine and is not misspelled

Try the other code if you like

Are you sure the animation you use works well

Try another animation that you know works well

I do not speak English

I find it difficult to communicate with others

I hope that the idea reached you

Add prints, see if they print. If it doesn’t print then we know that the animation isn’t the problem.
Try this code:

local players = game:GetService("Players").LocalPlayer
local character = players.Character
local animation = script.Parent:WaitForChild("Animation")
local loadAnim = character.Humanoid:LoadAnimation(animation)

print("Before Tool is activated")
script.Parent.Activated:Connect(function()
    print("Tool Activated") 
	loadAnim:Play()
end)