game.Players.playerAdded not working

Hey Roblox devs. I have a game.Players.PlayerAdded function in my game, in order to access a player through a server script. Here’s my code in a nutshell:


print("server script is active") --[[this is for me to know when the server script
 is active
--]]
game.Players.PlayerAdded:Connect(function(player)
     print("joined")
end)

The problem is, when I join it prints “server script is active” like it’s supposed to, but it doesn’t print “joined.” I’m thinking it’s most likely because of the time difference between when I join, and when the script is active.

Please let me know if you have a solution to this, and if this doesn’t make sense let me know and I’ll try to explain better.

2 Likes

So you’re yielding the script before connecting PlayerAdded?

This may be because you are using a local script, try using a regular script and using that instead. Let me know how it goes.

2 Likes

This seems to be a bit of weird issue. Theoretically this shouldn’t happen and you should be able to see that message, but for some reason you’re not. I have an idea in my head as to why this may be happening, but I’m not sure.

Can you try this script below and then send the console output from studio (not the in game console).

print("server script is active") --[[this is for me to know when the server script
 is active
--]]
game.Players.PlayerAdded:Connect(function(player)
     print("joined")
     player.CharacterAdded:Connect(function(char)
         print('Character added')
     end)
end)

This post could help:
https://devforum.roblox.com/t/playerserviceplayeradded-not-firing-intermittently/

1 Like

im not using a local script

aaaaaaaaaaa

what does yield mean

aaaaaaaaaaa

yield means that you are pausing, or waiting. eg. wait(5)

Where is your script under? If it isn’t already, just try moving it to ServerScriptService. Might be a stupid issue.

I think you should use it on ServerScriptService, Which it an useful Instance that you can use to detect things on the server for example detecting if a player is joining, like you did, So Try putting it on ServerScriptService.

Its also useful for detecting RemoteEvents for the Server, Save DataStores and etc…

Usually, this happens because the player was already in the game by the time the script binded the PlayerAdded event. An easy fix is to loop through the players once the connection has been set up. Here’s a video by Sleitnick that goes more into detail on this

This should fix it for you

local Players = game:GetService("Players")

local function playerAdded(player: Player)
	print("Player added", player)
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in Players:GetPlayers() do
	task.spawn(playerAdded, player)
end
3 Likes

Did you read the entire post? It says he is using a server script.

it doesnt need to be in serverscriptservice but its way optimized to do it that way

Was just a suggestion, hence why I had said he may have been using a local script. That is just a basic issue that could possibly occur, not sure if you read my post but I’m pretty sure I had just mentioned it in there.