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

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

Asked by
woodengop 1134 Moderation Voter
9 years ago

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

Log in to vote
2
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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).

0
So, Both can be used? woodengop 1134 — 9y
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. Redbullusa 1580 — 9y
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". Redbullusa 1580 — 9y
0
Oh ok thanks! I'll stick with PlayerAdded. woodengop 1134 — 9y
View all comments (4 more)
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. BlueTaslem 18071 — 9y
0
Then what do I use when I'm using a LocalScript? woodengop 1134 — 9y
0
"ChildAdded". Redbullusa 1580 — 9y
0
Thank you kind sir! woodengop 1134 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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.

0
I understood that part, no need to repeat it. woodengop 1134 — 9y

Answer this question