Attempt to index nil with humanoid

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local animation = script.Parent:FindFirstChild("Animation")
local PlayAnim = character.Humanoid:LoadAnimation(animation)

local canPlay = true

local debounce = 1.5

script.Parent.Activated:Connect(function()
	if canPlay then
		canPlay = false
		PlayAnim:Play()
		wait(debounce)
		canPlay = true
	end
end)

code is for whenever you use a gear, it plays that animation.
the error I get is attempt to index nil with humanoid. Anyone know a fix?

1 Like

The character hasn’t loaded yet.

local character = player.Character
if character==nil then
	player.CharacterAdded:Wait()
	character = player.Character
end

or simply just
local character = player.Character or player.CharacterAdded:Wait()

1 Like

You should always use WaitForChild() with a repeat/timeout value.

local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local humanoid = character:WaitForChild("Humanoid",3);

You might need to wait untill the character loads into the game using

player.CharacterAdded:Wait()

Second line of your code could be replaced with:

local character = player.Character or player.CharacterAdded:Wait()

Also as @dduck5tar stated you should wait until the humanoid loads inside the character model using:

local humanoid = character:WaitForChild("Humanoid")

From my knowledge I know that this error code mainly means the path to the object is incorrect or, it has not loaded yet. Try adding :WaitForChild()

edit: oops meant to reply to @Badged1 sorry!

1 Like

The post has been already edited

You can do it instantaneously do by doing :FindFirstChildOfClass and skip it on the Humanoid.

local Players = game.Players
local Animation = script.Parent:FindFirstChild("Animation")
local Humanoid = Players:FindFirstChildOfClass("Humanoid")
local Play_The_Animation = character.Humanoid:LoadAnimation(animation)

This is how the 1st 4 lines should be.

The rest of the script should be:

local Players = game.Players
local Animation = script.Parent:FindFirstChild("Animation")
local Humanoid = Players:FindFirstChildOfClass("Humanoid")
local Play_The_Animation = character.Humanoid:LoadAnimation(Animation)
local Can_Play = true
local Debounce = 1.5

script.Parent.Activated:Connect(function()
	if Can_Play then
		Can_Play = false
		Play_The_Animation:Play()
		task.wait(Debounce)
		Can_Play = true
	end
end)

It could work, if it doesn’t work, you probably copied the 1st script and not the full script.

How would you find a humanoid inside the players service? (There are only player instances)

1 Like

Well, you know in my Button Simulation Game, you know that the hit.Parent is a Instance, by adding :FindFirstChildOfClass it directly to the Character and you can see anything, anything that you can, but some of these you can’t, like the Humanoid, in this version of the :FindFirstChildOfClass, you can see anything, and here’s the reason why it’s not working when doing on the, you can’t do it manually, by doing game.Players.LocalPlayer.Character.Humanoid (Well technically yes on a LocalScript because it has a permission to allow to see the LocalPlayer which permits to Kick The Player That Wanted To Quit The Game If They Are Bored, or by Going To The Character, but in fact you need to manually type it if it’s a serverscript.).

Like This Script:

local config = script.Parent.Config
script.Parent.Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("Rebirth") then
				if plr.leaderstats.Cash.Value >= config.Cost.Value then
					plr.leaderstats.Multiplier.Value = plr.leaderstats.Multiplier.Value + config.Amount.Value
					plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - config.Cost.Value
					plr.leaderstats.Multiplier.Value = plr.leaderstats.Rebirth.Value + plr.leaderstats.Multiplier.Value-1 
					print("You got "..plr.leaderstats.Rebirth.Value.." Multiplier and lost "..config.Cost.Value.." Cash. So now you have "..plr.leaderstats.Cash.Value.." Cash and "..plr.leaderstats.Multiplier.Value.." Multiplier.")
				end
			end
		end
	end
end)

script.Parent.Part.GUI.TextLabel.Text = "$"..config.Cost.Value.." = x"..config.Amount.Value

Edit: Edited. Also, read the parentheses that I posted.

Bro, but what does it has to do with op’s problem? Take your pills, btw 4 nested ifs :skull: :skull: :skull:. You absolutely can access the humanoid of a character on a server.

Please make sure to read the parentheses.

I’ll make as well do it on the Quotes.

The issue with OP’s post is that the humanoid hasn’t loaded in yet, and therefore returns nil when referenced.

Your method, whilst useful when handling objects that have the potential to be named unconventionally, won’t work, as :FindFirstChildOfClass() returns nil if the object isn’t found. Same problem.

Instead, when using your method, you should be trying something like:

local character : Model = plyr.Character or plyr.CharacterAdded:Wait()
local hum : Humanoid = character:FindFirsChildOfClass("Humanoid")

while not hum do
    hum = character:FindFirsChildOfClass("Humanoid")
    task.wait()
end

The thread will not execute past this point whilst there isn’t a Humanoid-class object present.

Hope this helps.

local player = game:GetService("Players").LocalPlayer
local character = script.Parent.Parent -- script.Parent = Tool, script.Parent.Parent = The Player
local animation = script.Parent:FindFirstChild("Animation")
local PlayAnim = character:WaitForChild("Humanoid"):LoadAnimation(animation)

local canPlay = true

local debounce = 1.5

script.Parent.Activated:Connect(function()
	if not canPlay then
		canPlay = false
		PlayAnim:Play()
		wait(debounce)
		canPlay = true
	end
end)

I would suggest replacing:

local character = player.Character

With:

local character = player.Character or player.CharacterAdded:Wait()

Basically the second one checks if the character is loaded first and if it isn’t it waits for the character to load and then assigns the character to the variable.