Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the animation won't play on tool if its clicked?

This is the script i used:

local Tool = script.Parent
local Player = game.Players.LocalPlayer

Tool.Activated:connect(function()
	local animTrack = Player.Humanoid:LoadAnimation(script.Parent.Animation) 
	animTrack:Play()
end)

This is the error in the Output:

Players.Exonl_Roblox.Backpack.Sword.Script:5: attempt to index nil with 'Humanoid' - Server - Script:5

I am learning to script but i don't know what i did wrong

Help please.

2 answers

✓ Accepted answer

This one should work V

local Tool = script.Parent
local Player = game.Players.LocalPlayer

Tool.Activated:Connect(function()
    local animTrack = Player.Character.Humanoid:LoadAnimation(script.Parent.Animation) 
    animTrack:Play()
end)

You forgot to index character. Humanoids are not parented to player objects. You can also find character by getting Tool's parent.

Humanoid:LoadAnimation() is deprecated by the way, you probably should use Animator:LoadAnimation() instead. (Animator is parented to Humanoid by default)

:connect() is also deprecated. I doubt there's any difference to be honest. The new one is :Connect(). Just so you know.

local Tool = script.Parent
local Player = game.Players.LocalPlayer

Tool.Activated:Connect(function()
	local Character = Tool.Parent
	local Humanoid = Character:FindFirstChild("Humanoid")
	if Humanoid ~= nil then
	    local animTrack = Humanoid.Animator:LoadAnimation(script.Parent.Animation) 
	    animTrack:Play()
	end
end)

1 comment

0

Sorry it didnt work

You have to put "Player.Character.Humanoid" because, the humanoid is part of the character not the player.

1 comment

0

sorry didnt work

This discussion is preserved as it was. New answers, comments, and votes are closed.