Getting an error when playing animation

I am making a sword for someone and I made a script that is supposed to play an animation when the tool.Activated event fires. But I am getting this error that I can’t figure out what is causing.

local Tool = script.Parent
local track = nil
local Animation = Tool.Animation
local humanoid = Tool.Parent:FindFirstChild("Humanoid")

script.Parent.Activated:Connect(function()
	track = humanoid:LoadAnimation(Animation):Play() -- This is what I am going to use the to play the animation
	
end)

Can someone help me fix this error?

1 Like

You can’t execute the :Play() method inside the initialization of a variable; you’ll have to call it on a separate line.

track = humanoid:LoadAnimation(Animation)
track:Play()

Still getting this error…

Script:

local Tool = script.Parent
local track = nil
local Animation = Tool.Animation
local humanoid = Tool.Parent:FindFirstChild("Humanoid")

script.Parent.Activated:Connect(function()
	track = humanoid:LoadAnimation(Animation)
	track:Play()
end)

It looks like you have your script parented to the handle of the tool, so script.Parent does not direct to the tool.

local Tool = script.Parent.Parent
local Humanoid = Tool.Parent:FindFirstChild("Humanoid")

local Animation = Tool.Animation
local Track = Humanoid:LoadAnimation(Animation)

Tool.Activated:Connect(function()
	Track:Play()
end)

No it is parented to the tool.

Oh, the tool is named Handle, and I see my mistake now. The parent of the tool is actually Backpack when the player first spawns.

local Tool = script.Parent
local Animation = Tool.Animation
local Humanoid, Track

Tool.Equipped:Connect(function()
	Humanoid = Tool.Parent:FindFirstChild("Humanoid")
	
	if Humanoid then
		Track = Humanoid:LoadAnimation(Animation)
	end
end)

Tool.Activated:Connect(function()
	if Track then
		Track:Play()
	end
end)
1 Like

This is actually valid. Trying to use the track variable will not do anything however because Play does not return anything. So for this specific circumstance, separating the statements would be needed.

As for the error, it’s a literal error. The script is detecting nil being passed to LoadAnimation which means that the code is being executed before the instance replicates. In this instance, just use WaitForChild and the problem should be resolved. Same need goes with the Humanoid.

2 Likes

So use :WaitForChild() for both the animation, and humanoid instances?

I happened to fix the error but the animation won’t play at all! Someone please help me …

local Tool = script.Parent
local track = nil
local Animation = Tool.Animation
local humanoid = Tool.Parent.Parent.Character:WaitForChild("Humanoid")

script.Parent.Activated:Connect(function()
	
	track = humanoid:LoadAnimation(Animation)
	track:Play()
end)

This happened with another person recently, I think that roblox May have messed something up.You should ask a admin to see what’s wrong.

1 Like

How should I ask one? Post something in engine bugs? Or mention / ping an admin?

1 Like

Are you sure many people are really complaining about it? I DEFINITELY don’t want to lie to a staff member.