PlayerAdded function won't work

The script below is a LocalScript and it prints ‘1’, but not ‘2’.

print("1")
game:GetService("Players").PlayerAdded:Connect(function(player)
	print("2")
end)

How would I get the script to print ‘1’ and ‘2’?

It will only work if another player joins the game (when using the PlayerAdded in the client it doesn’t count yourself joining), you want to do that in a normal Script so it does actually print “2” when you join

2 Likes

To make sure it also detects the players who have already joined the game before the LocalScript code started running (which includes the player who is running the LocalScript code in this case), you could create a loop that looks through the Players service and activates the same function for each player that is already there:

Example

local Players = game:GetService("Players")
local player = Players.LocalPlayer --[[ If you just need to refer to the player
who is running the LocalScript code, then you could refer to the LocalPlayer
without needing to use the PlayerAdded event ]]--

local function onPlayerJoin(player)
    -- Continue
end

for _, player in Players:GetPlayers() do -- Gets all the Players in the game
    task.spawn(onPlayerJoin, player)
    -- Activates the "onPlayerJoin" function for each player
end

Players.PlayerAdded:Connect(onPlayerJoin)
--[[ Activates the "onPlayerJoin" function for each player that joins after the
LocalScript code started running --]]
1 Like

I tried using a RemoteEvent to put the code in a normal script to fire code in the local script, but it didn’t work

This didn’t work either, nothing happened

Hmmm, that’s odd. Where is the LocalScript code running from?

For reference, I just opened up a completely brand new starter baseplate place, included the following code in a LocalScript, placed it into the StarterPlayerScripts container, and it worked every time upon starting a playtest:

local Players = game:GetService("Players")
local player = Players.LocalPlayer --[[ If you just need to refer to the player
who is running the LocalScript code, then you could refer to the LocalPlayer
without needing to use the PlayerAdded event ]]--

local function onPlayerJoin(player)
	print(player.Name.." has joined the game!")
end

for _, player in Players:GetPlayers() do -- Gets all the Players in the game
	task.spawn(onPlayerJoin, player)
	-- Activates the "onPlayerJoin" function for each player
end

Players.PlayerAdded:Connect(onPlayerJoin)
--[[ Activates the "onPlayerJoin" function for each player that joins after the
LocalScript code started running --]]

Image Examples

LocalScripts are loaded and executes when the player joins.
So its not possible to detect when the player itself joined since the script runs when the player is joined and loaded.
The script will detect only when a new player joins the game. You can test this out if you create a localServer, join a player, wait a couple of seconds and the join another one. The first one’s output will be:

1
2

And the second joined player’s output will be

1

Becouse there is no a new player join when he joins, loads and runs the localScript.

If you want to achieve the detection when the player joins and prints(2) you can simply do
print(2)
Just that. The player joins and it will print 2

I hope this helps. If you have questions, I have a mistake or something another, feel free to reply :slight_smile:

1 Like

It’s worth to note this is a time-related issue, you need to bind the connection before the player actually loads in. So if for example above the PlayerAdded code you have a lot of module require calls, or other delays(such as WaitForChild), there’s a possibility it wont fire.

The reason it doesn’t fire on the client is that in order for a client script to run, the client must have loaded in, and if they have loaded in, that event has already fired and the developer can’t “catch” it with the code that runs after it(they can only detect players that join after them).

Also if you want to detect yourself joining on the client all you have to do is make a client script, put it inside game.StarterPlayer.StarterPlayerScripts, which will replicate it to game.Players.LocalPlayer.PlayerScripts that contains client scripts that run only once, and put a print("2") into it.

2 Likes