What is wrong with this code?

I created a local script in StarterPlayerScripts to print in the dev console whenever a person joins the game, but the code is not working? Does anyone know why? There is no errors in the output.
My code:

4 Likes

It’s probably because the player already has loaded in the game, and therefor this event never fires.

Does it not work entirely? Or just for the first player that joins in the game?

2 Likes

Try making a server script and putting that code inside. Put the script in ServerScriptService and have it print Player.Name .. " has joined the server" instead of script.Parent.Parent.Name.." has joined the server

Only use an event like PlayerAdded for one server script in ServerScriptService.

This is a LocalScript inside a player, so by the time it’s added and running, the player has already loaded into the game and the PlayerAdded event will never fire. To fix it just get rid of the first and third lines.

It does not work entirely. :frowning:

will this work for every player that joins the game then? won’t it only work for the first one?

Note that this would only print out to the client. (although if its just a printout to the console, i doubt it would matter if it is visible to the server or to other players; although if you intend to do anything more inside that code block, you may want to move it to the server)

If it is in a localScript, this script will replicate to every player and print out that statement.

Events fire every time they’re triggered. Your connection tells the event “hey, every time a player joins, run this code”. So, every time someone joins, it would print that they joined on the server.

2 Likes

The PlayerAdded event doesn’t work in local scripts. These types of events are server sided.

Insert a script > Place it into Server Script Service > Place the code in there.

Also :

local Players = game:GetService("Players") 

Players.PlayerAdded:Connect(function(Player,...)
print(Player.Name.." has joined the server.")
end)
6 Likes

I putted a local script in ServerScriptService and copied the code except I changed script.Parent.Parent.Name to Player.Name, but it still did not work.

The event works on the client.

  • “Up until recently, this event didn’t work on the client (in Localscripts), but this has been changed”

I’m not sure how recent is recently, but this is not the issue.

3 Likes

Yep it works. I tested it in solo and also in a server. Thanks! :grinning: :+1:

1 Like

This is not the case. The reason it doesn’t fire for the LocalPlayer is because they’re already in-game (and the event has already fired) by the time the script runs. It will fire for other players perfectly fine.

3 Likes

Ah! That is another reason why it doesn’t work. The client loads before the event is fired which makes perfect sense. :+1:

I do wonder though, why do they let this event be used on the client if there is no possible way it will ever be fired in any localscript?

[EDIT] @posatta i didnt know that other players being added replicated to the client, but that makes sense

It will fire when other players join, just not the client the script is running for.

3 Likes

for i, v in next, game.Players:GetPlayers() do
– Players currently in game
end

game.Players.PlayerAdded:connect(function(Player)
– New players that just joined the game.
end)

wouldn’t then my previous code work if I added a game:IsLoaded() on line 2?

try this:

game.Players.PlayerAdded:Connect(function(player)
print(player.Name .." has joined the server")
end)