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.
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)
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
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.