Equip/Unequip animation not working

I have a script inside a tool that plays when a player equips/unequips it. However, I’m getting an error:

attempt to index nil with 'Character'

Here’s my code:

local tool = script.Parent;
local equipAnimation = Instance.new("Animation");
local unequipAnimation = Instance.new("Animation");
equipAnimation.AnimationId = "rbxassetid://8350902779";
unequipAnimation.AnimationId = "rbxassetid://8350914446";

local equipAnimationTrack = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):LoadAnimation(equipAnimation);
local unequipAnimationTrack = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):LoadAnimation(unequipAnimation);

tool.Equipped:Connect(function()
    print("EQUIPPED");
    equipAnimationTrack:Play();
end)

tool.Unequipped:Connect(function()
    print("UNEQUIPPED");
    unequipAnimationTrack:Play();
end)
1 Like

are you sure the script is a localscript

Oh. It has to be a local script? I have it as a Script.

I thought that local scripts only show to the local player and not everyone else. Trying to make it so that the animation shows for all the players.

LocalPlayer can’t be referenced by the server, so it’ll return nil. Only the client can reference the instance.
The wiki article explains well how to reference it

If you still want it as a script just do

local tool = script.Parent;
local equipAnimation = Instance.new("Animation");
local unequipAnimation = Instance.new("Animation");
equipAnimation.AnimationId = "rbxassetid://8350902779";
unequipAnimation.AnimationId = "rbxassetid://8350914446";


tool.Equipped:Connect(function()
local Character = script.Parent.Parent
animation = Character:FindFirstChild("Humanoid"):LoadAnimation(equipAnimation):Play()
end)
tool.Unequipped:Connect(function()
local Character = workspace:FindFIrstChild(script.Parent.Parent.Parent.Name)
animation = Character:FindFirstChild("Humanoid"):LoadAnimation(unequipAnimation)
end)
1 Like
local equipAnimation = Instance.new("Animation")
local unequipAnimation = Instance.new("Animation")
equipAnimation.AnimationId = "rbxassetid://8350902779"
unequipAnimation.AnimationId = "rbxassetid://8350914446"

local equipAnimationTrack = nil
local unequipAnimationTrack = nil

local humanoid = nil

tool.Equipped:Connect(function()
   humanoid = tool.Parent:WaitForChild("Humanoid")
   equipAnimationTrack = humanoid:LoadAnimation(equipAnimation)
   unequipAnimationTrack = humanoid:LoadAnimation(unequipAnimation)

    equipAnimationTrack:Play()
end)

tool.Unequipped:Connect(function()
    unequipAnimationTrack:Play()
end)

try that

3 Likes