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

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
0
Now I Get This Error :/ Players.AmazingAmazingArthur.Backpack.Gun.ClientHandler:11: attempt to index nil with 'WaitForChild' AmazingAmazingArthur 15 — 4y
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago

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!

Answer this question