Attempt to index nil with 'Humanoid'?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I need to get the Humanoid from a Player.

  2. What is the issue? Include screenshots / videos if possible!
    44: attempt to index nil with ‘Humanoid’

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Target is a HumanoidRootPart.

function main()
	local target = findTarget()
	if target then
		local char = target.Parent:FindFirstChild("Character")
		local human = char.Humanoid -- HERE ERROR
		if human and human.MoveDirection.Magnitude > 0 then
			target.Parent.Humanoid.Health -= 5
		end
	else
		print("hided")
	end
end

The error is appearing cause humanoid isn’t loaded in the character.
Try getting Humanoid with delay: char:WaitForChild("Humanoid").

Yes it is. But I still can’t figure out why it doesn’t work.
44: attempt to index nil with ‘WaitForChild’.

means the character doesnt exists.
looking at your script, you are making a kind of gun?

char is equal to nil, so target.Parent has no children named “Character.”

i think this may work for you:

function main()
	local target = findTarget()
	if target then
		local char = target.Parent:FindFirstChild("Character")
		if (not char) or (not char:FindFirstChild("Humanoid")) then return end;
		local human = char.Humanoid -- HERE ERROR
		if human and human.MoveDirection.Magnitude > 0 then
			target.Parent.Humanoid.Health -= 5
		end
	else
		print("hided")
	end
end
1 Like

Yes, but it’s an NPC. It should reduce the health of the player if he moves.

But how else can I get a Humanoid to understand if it is moving?

what? i dont understand


I’ve modified your code to what it should be:

function main()
	local target = findTarget()
	if target then
		local human = target:FindFirstChild('Humanoid') -- HERE ERROR
		if human and human.MoveDirection.Magnitude > 0 then
			human.Health -= 5
		end
		human = nil
	else
		print("hided")
	end
end

The above assumes that the target is a character. If the target is a player, do this instead:

function main()
	local target = findTarget()
	if target then
		local human = target.Character:FindFirstChild('Humanoid') -- HERE ERROR
		if human and human.MoveDirection.Magnitude > 0 then
			human.Health -= 5
		end
		human = nil
	else
		print("hided")
	end
end

isnt it FindFirstChild?


Can Humanoid be obtained from HumanoidRootPart?

No, but you can obtain humanoidrootpart from humanoid.

1 Like
humanoidRootPart.Parent:FindFirstChild('Humanoid')

You may need to use WaitForChild if you’re doing this right as a character loads.

if you want to use parents, do

humanoidRootPart.Parent:FindFirstChildOfClass('Humanoid')

this is more efficient because it only searches an instance with a humanoid class.

1 Like

That working:

function main()
	local target = findTarget()
	if target then
		local human = target.Parent:FindFirstChildOfClass("Humanoid")
		if human and human.MoveDirection.Magnitude > 0 then
			target.Parent.Humanoid.Health -= 5
		end
	else
		print("hided")
	end
end

do

function main()
	local target = findTarget()
	if target then
		local human = target.Parent:FindFirstChildOfClass("Humanoid")
		if human and human.MoveDirection.Magnitude > 0 then
			human.Health -= 5
		end
	else
		print("hided")
	end
end

image

Code
local character = script.Parent
character:WaitForChild('Humanoid')

local name = {}
local class = {}

for i=1,1000 do
	local start = os.clock()
	character:FindFirstChild('Humanoid')
	table.insert(name,os.clock()-start)
	start = nil
end

for i=1,1000 do
	local start = os.clock()
	character:FindFirstChildOfClass('Humanoid')
	table.insert(class,os.clock()-start)
	start = nil
end

local nameAverage = 0
for _,n in name do
	nameAverage+=n
end
nameAverage/=#name

local classAverage = 0
for _,n in class do
	classAverage+=n
end
classAverage/=#class

print('Name: '..nameAverage)
print('Class: '..classAverage)

I actually find that name is faster. But it doesn’t really matter which you use.

well yes i dont mind people who use these methods, but FindFirstChildOfClass only searches instances by their classnames. so it’s 2 different functions :+1:

1 Like

That’s true, each have their own use. But the reason using the name is faster is because the name is a direct index, where as to get the class, it has to iterate through every child to compare their ClassName. In addition to that, FindFirstChild also has a second optional argument which would, if set to true, iterate through all descendants, instead just indexing by name.

So unless you’re using other things within the character that would have the name of Humanoid, I believe FindFirstChild is better.

1 Like

true true, i told him to use FindFirstChildOfClass incase he’d include other characters than has a humanoid with a name other than “Humanoid”.

1 Like

Fair enough then.

I would’ve liked your reply instead of manually replying, but I have 9 hours before I can like another post.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.