Players.PlayerAdded event Does NOT Respond to the Code

Reproduction Steps

  1. Open your Roblox Studio application;
  2. Open an existing place/create a new place using templates;
  3. Go to the Explorer tab and insert a LocalScript in available classes such as StarterGui, StarterPlayer etc (in the following, I have used the StarterGui);
  4. Opening up the script in the editor, write the first variable “Players” to indicate the game’s service and in the next lines, create an event using Players.PlayerAdded;
    3.1. Now, with the event being written, create a simple mechanic that can identify if it works or not, for example, printing the player’s name;
  5. Apply all of the edits in your script;
  6. Test the experience.

Expected Behavior

I expected that once joining the experience, it would eventually fire up the statements of the LocalScript that I wrote.

Actual Behavior
After setting up a simple mechanic to identify whether it works or not, and playing the experience inside of the engine, it does not print anything in both Output or Developer Console.

Workaround

A workaround that can help a bit is if we create another variable called “Player” with the configuration as printing the LocalPlayer’s name/replacing Players.PlayerAdded with Players.PlayerRemoving (although, it would only work in case the player leaves the place):

Issue Area: Studio
Issue Type: Other
Impact: Critical
Frequency: Constantly
Date First Experienced: 2023-01-05 00:19:55 (UTC-3)
Date Last Experienced: 2023-01-24 00:01:25 (UTC-3)

2 Likes

We’ve filed a ticket to our internal database, and we’ll follow up when we have an update.

Thanks for the report!

4 Likes

Did connecting to PlayerAdded from a LocalScript provide the local player being added at some point? I can’t ever remember it doing so, and wouldn’t expect it to considering that the player instance already has to be added in order for your local scripts inside StarterGui (which are copied into PlayerGui, into your player) in order to work. LocalPlayer is guaranteed to exist in a LocalScript, even in something like ReplicatedFirst.

3 Likes

Sorry, isnt this intended?

The PlayerAdded event would fire BEFORE the localscript is cloned into playerGui

6 Likes

This has always been this way I’m pretty sure, particularly in Studio IIRC. PlayerAdded fires before your LocalScript starts running. You need to iterate over the current players in addition to the PlayerAdded event.

4 Likes

Aye, everybody!

Apparently, a lot of you in the replies responded to my topic but with a bit of being confused in regards of the description as well as I created a little sample as LocalScript in areas available for the instance:

While doing research of mine, the event is an RBXScriptSignal and possible to be captured in normal scripts in classes like ServerStorage, ServerScriptService, and little other areas… Essentially, it is possible for us now to use in LocalScripts, according to the Roblox documentation:

https://create.roblox.com/docs/reference/engine/classes/Players#PlayerAdded

Following up its context, the method actually have worked in the past whenever developing personal Roblox projects while featuring many other areas in the code to run. Suddenly, it begin to stop being functional somewhere closely to 2023 (this year). I thought it was a glitch, but during recent experiments, it seemed that it wasn’t.

Overall, I hope this can explain better for the forumers! Quite an incomprehension at first, but, hey, it is totally fine and the best is just to respond with at least an elaborative answer. :face_with_monocle:

@Dogekidd2012 Analysing your question (please correct me if I am wrong or a misinterpretation) , yes, the event actually provided the player joining the experience, yet it did not print telling to us in the Output that he was there.

The alternatives to demonstrate if he was is as if we printed as leaving/making a different technique (which has been explained in the primary topic). But, that does not change my point stating that it still doesn’t print the player in the LocalScript…

Let’s design another illustration… If I create a LocalScript in the StarterGui class with the functionality to print the player’s username in a TextLabel and do the following:

local Players = game:GetService("Players")

local TextGui = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextLabel

local function onPlayerAdded(player)
    print(player.Name .. " has joined the game")
    TextGui.Text = player.Name
end

Players.PlayerAdded:Connect(onPlayerAdded)

This does not work at all, same as if we do the simple strategy by excluding the local function and making the code inside of the event Players.PlayerAdded:Connect(function(player).

“How about a normal script, it would work, right?” – No, it wouldn’t be able to capture the property LocalPlayer and PlayerGui for additional information on the code line.

It does work.

Just not on the player running the script, because the PlayerAdded event for them gets fired before this line is executed:

(like others have said)

If you test run with multiple players, let player1 load in before letting player2 join, you will see that the event fires as expected.

I joined an experience with the LocalScript containing the event being stated but oddly that you have to wait for another character to be added. It should print both players instead of needing to wait for another (player2) to join first… I don’t think many of developers are aware of so and it can create a perspective of not being able to work…

How?
Roblox doesn’t know that you want to listen to the PlayerAdded event until after you(player1) have been added.

This is intended behaviour, the PlayerAdded event fires when a new player has been added. Not for players that have already been added. If it suddenly changes then that would break most if not all use of PlayerAdded.

(others saying that this is intended behaviour

You will have to find another way to achieve what you want. An example solution has already been posted:

The best solution for you depends on what you want to achieve exactly, you should post on #help-and-feedback:scripting-support.

2 Likes

Thank you! Now, I got it properly. :slightly_smiling_face::pray:

This doesn’t happen on my end though. Are you sure this is expected behavior?

Are you doing PlayerAdded in a regular script? That runs before the player joins so then it does work for all players.

It wouldn’t make sense for this to run for players that have already joined, because what if you only connect to the PlayerAdded event after a wait of multiple seconds? Then one connection will run more times than another connection? It should only run when a player joins (like the name implies) not also go through players that have already joined.

This is a feature working as intended.

To get all players you have to iterate over them first before connecting to .PlayerAdded

local Players = game:GetService("Players")

local function onPlayerAdded(player)
	-- // YOURCODE
end)

for _, v in ipairs(Players:GetPlayers()) do
	coroutine.wrap(onPlayerAdded)(v)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Ah, yes, it is. I think I misread the post.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.