Is there really any difference between these 3?

Hi. I’m genuinely curious about this. I’ve learned to check if a player touched a part via this:

if game.Players:FindFirstChild(hit.Parent.Name) then

I know the most common line is this:

if game.Players:GetPlayerFromCharacter(hit.Parent) then

And there’s also this:

if hit.Parent:FindFirstChild("Humanoid") then

And I’m wondering, are there really any downsides to using FindFirstChild? I know FindFirstChild is slightly slower but it’s not noticable.

1 Like

The only downside I can see for the last one is if there are different NPC’s in the game containing humanoids but the other two is what you said.

1 Like

I guess. And I’ve seen a few people making others use :GetPlayerFromCharacter instead. It’s weird how 3 lines work the same huh. Kind of like how people complain that you can do a lot of different things with the same outcome in c++ but way easier.

There are a few edge cases which are why you shouldn’t ever use the first one:

1- In a case where there is a non-Player child of the players service that shares a name with a real player/character, you wouldn’t want to use the first one as you can’t guarantee that players:FindFirstChild(string) returns a player unless you specify that the returnedValue:IsA(‘Player’). Even in that case, though, you would have to iterate through the player service’s children to find a corresponding player. At that point, you might as well use players:GetPlayerFromCharacter.

2- Similarly to case 1, if you have a part as a descendant of the workspace whose parent shares a name with a player, and that part is passed as otherPart to the touched event, it would return the real player.

So, players:GetPlayerFromCharacter() ensures that:
1- The character belongs to a player, and
2- A player object or nil is returned

1 Like

Hey, I’m not familiar with the new display name thing. Does the Player take the name of the display name (i.e cody) and in that case couldn’t that cause issues as well, if two users with the same display name join the game? That would be another strong case for option 2

Nope, the display name is just a property of the player (luckily). The player still retains their username.

1 Like