PlayerAdded event not firing from the client

I was trying to make a camera interpolation when Player joins the game in a local script inside of StarterGui. I tried using PlayerAdded event in that local script and then print something when player is added, but that seems not working. My output is empty.
Here is the code.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function()
    print("Player is added")
end)

I’ve tried searching on YouTube, scriptinghelpers, Roblox Wiki, but these sites didn’t help. I was reading on ROBLOX Wiki about Player Added event and there’s a note which says “Up until recently, this event didn’t work on the client (in Localscript s), but this has been changed

I just can’t find the problem. Does anybody knows how can I fix this?

4 Likes

Are you listening for the local player itself to join, or other players?

Local Player. However the max count of players in the server is “1”, so…

Local scripts run after the player is created. This means that the PlayerAdded event will not fire for the local player joining. Instead, just put the code that would otherwise be under PlayerAdded in the local script and it will run when the player joins.

2 Likes

Do you really need to listen to the event when the max player count is one? What are you trying to do?

Once player joins the game, his camera will tween from one part to another in a loop. That’s the reason. Unless you got a better idea?

1 Like

On top of this, the Player instance itself can be got through Players.LocalPlayer, which will be available immediately in a LocalScript.

OP - You do not need to listen for the player being added on the client. By definition, when LocalScripts run on your machine, you’ve already been added to the game.

8 Likes

When the local scripts run, the player has already joined the game.

1 Like
repeat
	wait()
until game.Players.LocalPlayer

Is this what you want?

1 Like

I didn’t know that, thank you.

Exactly, the script don’t load in fast enough to detect the current player added.

1 Like

You shouldn’t do repeat wait() until, since this executes a wait before checking. Instead do this:

while not game.Players.LocalPlayer do
    wait()
end

This way, it doesn’t wait if the player already exists. It adds up!

However, you should expect Players.LocalPlayer to exist on init in local scripts, most of the time.

3 Likes

So, there’s a chance that Players.LocalPlayer will be nil at some given time in a LocalScript? If so, are there any rules that dictate when the property will and won’t be nil? I’ve always thought that the property will never be nil in a LocalScript.

Couldn’t you theoretically do:

game:GetService("Players").ChildAdded:wait()

?

No. I’m not sure why that code was posted either (the one about waiting until the existence of the player via a repeat statement). That’s a relatively pointless measure. LocalScripts only run after LocalPlayer is initialised. LocalPlayer is implicitly available to LocalScripts.


LocalScripts are still replicated and ran after LocalPlayer is completely set up, same as before.

LocalScripts should be detecting game.Players.LocalPlayer without issues when it starts. If LocalPlayer is nil when a localScript starts, it would present an issue - please report this case.

‘game.Players.LocalPlayer’ will always be non-nil

2 Likes

I’m pretty sure that you won’t get a nil Players.LocalPlayer. My pet-peeve was at the repeat wait() until.

Yes, but you want to make sure LocalPlayer exists, and so you may miss some changes. I recommend being very careful doing this pattern.

1 Like

LocalPlayer will always exist for a LocalScript.

Good point! Missed what the OP was looking for.

Perhaps

if not game.Players.LocalPlayer then
     game.Players.ChildAdded:wait()
end
local Player = game.Players.LocalPlayer

(for a one-player server that they’re talking about)

In ReplicatedFirst scripts, it’s possible that it won’t (or that some of the descendants won’t be present immediately)

LocalPlayer is always available to me even in ReplicatedFirst scripts. If it’s not, I believe it’s a bug - LocalPlayer should be implicitly available in any and all LocalScripts.

3 Likes