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)
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)
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)
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.