Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
1
Answered by
memguy 161
3 years ago
Edited 3 years ago

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)
0
Sorry it didnt work Exonl_Roblox 2 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

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

0
sorry didnt work Exonl_Roblox 2 — 3y

Answer this question