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

Why my code doesnt print humanoid found even when i did all the necessary checking??

I even printed out the player name and double checked it in the workspace its the same!! and the output doesnt show any errors

game.Players.PlayerAdded:connect(function(player)
print(player.Name)
player:WaitForChild("Humanoid")
if player:FindFirstChild("Humanoid") == true then
print("Humanoid found!")
end
end)

2 answers

✓ Accepted answer

First of all, the Humanoid is located in the player's character, not the player itself. You must wait for the character to load, however, so you must use a CharacterAdded event:

player.CharacterAdded:Connect(function(character)

By all means, though, do not use WaitForChild on the character, as it is a property, and WaitForChild cannot be used on properties.

Also, connect is deprecated; use Connect instead. It is also unnecessary to use "== true" after FindFirstChild. You can simply use the following:

if example:FindFirstChild("example") then

However, if you'd like to check if FindFirstChild returns false, then you can use not.

Example:

if not example:FindFirstChild("example") then

Finally, I believe this link will be of help to you: https://www.robloxdev.com/articles/Writing-Clean-Code

Please indent your code. It makes it much more legible and comprehensible, therefore making it easier to fix or get help with.

Here is the final product:

game:GetService("Players").PlayerAdded:Connect(function(player) --When a player joins
    print(player.Name) --Print the player's name
    player.CharacterAdded:Connect(function(character) --When the character loads
	    if character:WaitForChild("Humanoid") then --If the humanoid exists inside of the character then proceed
	        print("Humanoid found!") --Print that the humanoid has been found
	    end
	end)
end)

4 comments

0

you could also instead use the CharacterAdded event connecting to a function

0

What i meant was to do Workspace:WaitForChild(plr.Name), not the character. I guess i wasnt specific enough.

0

What i meant was to do Workspace:WaitForChild(plr.Name), not the character. I guess i wasnt specific enough.

0

While that would work, it would be more efficient to use the built in event. And yes, EX, I somehow overlooked that. I'm going to update that now.

Your error on 4 line. Try this.

Line 3 useless, and humanoid belongs player.Character not player

game:GetService("Players").PlayerAdded:connect(function(player)
	print(player.Name)
	if player.Character:WaitForChild("Humanoid") then
		print("Humanoid found!")
	end
end)

3 comments

0

thats pretty dangerous, if the player character doesnt load before the statement runs it will throw an error, you should use WaitForChild instead to make sure that the character loads

0

You can't use WaitForChild on a property.

0

You can use it

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