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

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
lunatic5 409 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
you could also instead use the CharacterAdded event connecting to a function User#23365 30 — 5y
0
What i meant was to do Workspace:WaitForChild(plr.Name), not the character. I guess i wasnt specific enough. aazkao 787 — 5y
0
What i meant was to do Workspace:WaitForChild(plr.Name), not the character. I guess i wasnt specific enough. aazkao 787 — 5y
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. lunatic5 409 — 5y
Ad
Log in to vote
0
Answered by
HaveASip 494 Moderation Voter
5 years ago
Edited 5 years ago

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)
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 aazkao 787 — 5y
0
You can't use WaitForChild on a property. lunatic5 409 — 5y
0
You can use it HaveASip 494 — 5y

Answer this question