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

What is the Best Way to get a Player when Joined "ChildAdded" or "PlayerAdded"?

ChildAdded

I have seen Scripts where Users would use the "ChildAdded" instead of the PlayerAdded when a Player joins the Server, for Example:

game.Players.ChildAdded:connect(function(chl)
	print(chl.Name.." Has joined the game"
end)

This also works for Models, Parts, etc When moved to Workspace(or any other Containers).

PlayerAdded

But In most Scripts users would Use the PlayerAdded Which on the other hand Picks-up the Player THAT Joined the Server, Here is an Example:

game.Players.PlayerAdded:connect(function(plr)
	print(plr.Name.." Has Joined the game"
end)

Which is Better to use, and why?

2 answers

✓ Accepted answer

According to this, you would typically use PlayerAdded in a server script, whereas ChildAdded can be used either in a local or server script, because its use is more broad.

There's almost no difference between the two events. But if you were to test two server scripts with different events, be sure that you are adding nothing to game.Players, else the ChildAdded will fire (and the PlayerAdded event will not).

8 comments

0

So, Both can be used?

1

Any of them can be used in a server script, but the "PlayerAdded" event is typically used. I'd use it wherever possible, because the "PlayerAdded" event detects players, while the "ChildAdded" event detects ANY objects.

1

Same case with whether to use "game.Players:GetPlayers()" or "game.Players:GetChildren()". They're almost the same, but ":GetPlayers()" creates a table of all of the players, while ":GetChildren()" creates a table of all of the objects in "game.Players".

0

Oh ok thanks! I'll stick with PlayerAdded.

1

If some script adds objects to the Players service (they shouldn't, but they can) that will fire ChildAdded. That WON'T fire PlayerAdded, so you can assume that PlayerAdded is always given a real player. For whatever reasons, PlayerAdded doesn't work in a LocalScript.

0

Then what do I use when I'm using a LocalScript?

0

"ChildAdded".

0

Thank you kind sir!

In addition to what Redbullusa stated,

PlayerAdded only fires when a player was inserted into Players, whereas ChildAdded would fire whenever your put anything in Players, such as a Part, or Model.

Same thing with game.Players:GetChildren(). That gets all children, and then handy dandy game.Players:GetPlayers() only gets the Player objects in Players.

1 comment

0

I understood that part, no need to repeat it.

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