--// Variables
local Damage = script.Parent.Damage.Value
local Animation = script.Animation.Swinging
local sound = script.Sound.Swinging
local tool = script.Parent
local cooldown = script.Parent.Cooldown
local humanoid = tool.Parent:FindFirstChildWhichIsA("Humanoid")
--// Tool Events
tool.Activated:Connect(function()
local anim = humanoid:LoadAnimation(Animation) -- This doesn't work
anim:Play()
print("Swing has activated")
sound:Play()
wait(cooldown)
anim:Stop()
print("Swing has deactivated")
end)
I get an error saying attempt to index nil with LoadAnimation. Any help would be appreciated.
Try printing print(tool.Parent.Parent.Name) if it’s not referring correctly, you might need to add or remove a Parent. WaitForChild() shouldn’t be what you need.
Try removing Humanoid from my line of code and check if it’s still nil.
Or use FindFirstChildWhichIsA("Humanoid") if Humanoid might be a custom name.
But the variable is defined before that. It’s defined right when the server starts up(maybe it waits till it’s in the backpack if it’s in startertools), but it’s never defined when it gets equipped, unless you moved the humanoid variable in the activated function
I think you should go with WaitForChild. It’ll instantly return the object if it exists anyway.
You see, when the character’s model is created, CharacterAdded() fires but the model doesn’t have a humanoid yet. I’ve seen that happen already.
Just realized WaitForChild solves nothing.
Maybe try this?
--// Variables
local Damage = script.Parent.Damage.Value
local Animation = script.Animation.Swinging
local sound = script.Sound.Swinging
local tool = script.Parent
local cooldown = script.Parent.Cooldown
local humanoid
--// Tool Events
tool.Activated:Connect(function()
humanoid = tool.Parent:WaitForChild("Humanoid")
print(humanoid)
--If it doesn't print at all, humanoid isn't there and never gets added. Custom character problems?
local anim = humanoid:LoadAnimation(Animation) -- This doesn't work
anim:Play()
print("Swing has activated")
sound:Play()
wait(cooldown)
anim:Stop()
print("Swing has deactivated")
end)
If it’s a localscript then clearly :WaitForChild() is the way to go. Technically speaking script.Parent won’t need a WaitForChild but anything that is inside of the parent may very well need it. Even if you don’t need it know that it’s always a good practice to use it when trying to get something such as this.