players.PlayerAdded not working

So I just started creating a server script for my new game and it’s not working and i don’t know the reason why it’s not printing player when i do player.PlayerAdded

local players = game:GetService("Players") 

print('0')

local remotes = replicatedstorage:WaitForChild("Remotes")
local sharedmodules = replicatedstorage:WaitForChild("Modules")
local sharedassets = replicatedstorage:WaitForChild("Assets")


local servermodules = serverstorage:WaitForChild("Modules")


local starter_playervalues = {}
local starter_playerdebounces = {}


print("2")
players.PlayerAdded:Connect(function(player)  
	print("PLAYER")
end)

I’m guessing some of the things you are calling WaitForChild on don’t exist, so the PlayerAdded event isn’t connecting right away, and the player joins before it got connected.

1 Like

This is the kind of issues that you can easily solve by just figuring things out.

I’ll ask you some questions to see if anything works

  1. What’s serverstorage? Is it defined?
  2. Are the wait for child functions being solved? Or are they left hanging forever?

The wait for childs work and the serverstorage is where game:GetService(“serverstorage”)

Can you show the output? Maybe the script is erroring.

the script doesn’t have any errors cuz if it did i could fix them

try getting rid of all waitforchilds

it’s working now but I don’t know what the problem was

This is mainly due to how your script is ordered

When a player first joins the game, the WaitForChild functions are called first since they’re in high priority which will yield the script, and will pause until the objects have been found which is not connecting in time to your PlayerAdded event

Can you try this?

local players = game:GetService("Players") 

players.PlayerAdded:Connect(function(player)  
	print("PLAYER")
end)

print('0')

local remotes = replicatedstorage:WaitForChild("Remotes")
local sharedmodules = replicatedstorage:WaitForChild("Modules")
local sharedassets = replicatedstorage:WaitForChild("Assets")


local servermodules = serverstorage:WaitForChild("Modules")


local starter_playervalues = {}
local starter_playerdebounces = {}


print("2")

the player joined, but the waitforchilds were still waiting for the child, so the function didnt fire
difficult to explain for me