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

Error Attempt to index nil with 'Humanoid' Any Help?

For some reason I keep getting this error anybody able to help me?

Code:

local WeaponTool = script.Parent

local equiptanimation = Instance.new("Animation", WeaponTool)

equiptanimation.AnimationId = "rbxassetid://5721377231"

equiptanimation.Name = "EquipAnim"

local char = game.Players.LocalPlayer.Character

local humanoid = char.Humanoid

local animload123214 = humanoid:LoadAnimation(equiptanimation)

WeaponTool.Equipped:Connect(function()
	game.ReplicatedStorage.Equip:FireServer(WeaponTool.BodyAttach)
	
	char.Torso.ToolGrip.Part0 = char.Torso
	
	char.Torso.ToolGrip.Part1 = WeaponTool.BodyAttach
	
	animload123214:Play()
end)

WeaponTool.Unequipped:Connect(function()
	animload123214:Stop()
	
	game.ReplicatedStorage.DeEquip:FireServer()
end)

Error:

  11:39:34.952 - Players.AmazingAmazingArthur.Backpack.Gun.ClientHandler:11: attempt to index nil with 'Humanoid'

Thanks!

2 answers

The humanoid always takes time to load in, to fix this problem use Parent:WaitForChild("Child"), this waits for the child before executing any code. Example:

local part = workspace.Part
local child = part:WaitForChild("Decal")

Your fix:

local WeaponTool = script.Parent

local equiptanimation = Instance.new("Animation", WeaponTool)

equiptanimation.AnimationId = "rbxassetid://5721377231"

equiptanimation.Name = "EquipAnim"

local char = game.Players.LocalPlayer.Character

local humanoid = char:WaitForChild("Humanoid")

local animload123214 = humanoid:LoadAnimation(equiptanimation)

WeaponTool.Equipped:Connect(function()
    game.ReplicatedStorage.Equip:FireServer(WeaponTool.BodyAttach)

    char.Torso.ToolGrip.Part0 = char.Torso

    char.Torso.ToolGrip.Part1 = WeaponTool.BodyAttach

    animload123214:Play()
end)

WeaponTool.Unequipped:Connect(function()
    animload123214:Stop()

    game.ReplicatedStorage.DeEquip:FireServer()
end)

1 comment

0

Now I Get This Error :/ Players.AmazingAmazingArthur.Backpack.Gun.ClientHandler:11: attempt to index nil with 'WaitForChild'

What WideSteal321 said plus, you also need to wait for character itself. Replace line 11 with:

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

Paranoid people wait for Animator too, to make sure animations replicate to other clients.

local humanoid = char:WaitForChild("Humanoid")

humanoid:WaitForChild("Animator")

local animload123214 = humanoid:LoadAnimation(equiptanimation)

Hope this helps!

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