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

attempt to index nil with 'LoadAnimation' ?

Asked by 2 years ago

So I'm scripting a tool and the load animation is not working

Originally I had the animations loaded inside the functions it would work when you equipped the tool and break when you unequipped the tool.

now it just doesn't work at all

anyone able to help out?

local Tool = script.Parent

local AnimFolder = script.Parent.AnimFolder

local Weilder = Tool.Parent:FindFirstChild("Humanoid")
local IdleAnimation = Weilder:LoadAnimation(AnimFolder:WaitForChild("Idle"))

Tool.Equipped:Connect(function()

    IdleAnimation:Play()

end)

Tool.Unequipped:Connect(function()

    IdleAnimation:Stop()

end)

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago

When you equip the tool it's in your Backpack, (game.Players.xxXDuckzzzXcxcxcx.Backpack), if you look at it in explorer while the game is running, you will see that there is no Humanoid in Backpack, meaning Tool.Parent:FindFirstChild("Humanoid") would be nil, that's why there's error saying that you are trying to use :LoadAnimation function of non-existent instance.

When the tool is laying on the ground and you pick it up, it automatically goes to your character, so the humanoid would be Tool.Parent.Humanoid, but if the tool is in StarterPack then it's in your Backpack by default, that means we need to get the character and then the humanoid like Tool.Parent.Parent.Character.Humanoid.

local Tool = script.Parent

local AnimFolder = script.Parent.AnimFolder

-- if tool's parent is backpack (if it's in backpack) then get the player's
-- character and look for the humanoid there
-- else if the tool is not in backpack then it must be in player's character
-- (because it's equipped) so we use Tool.Parent:FindFirstChild("Humanoid")
local Weilder = if Tool.Parent:IsA("Backpack")
    then Tool.Parent.Parent.Character:WaitForChild("Humanoid")
    else Tool.Parent:WaitForChild("Humanoid")

local IdleAnimation = Weilder:LoadAnimation(AnimFolder:WaitForChild("Idle"))

Tool.Equipped:Connect(function()

    IdleAnimation:Play()

end)

Tool.Unequipped:Connect(function()

    IdleAnimation:Stop()

end)

Tool.Parent is Backpack. Backpack.Parent is Player (zxzxDuskxzxzx). Player.Character is the character. That's why Tool.Parent.Parent.Character.

Ad

Answer this question