Players.PlayerAdded event doesn't connect to function

I’ve been working on a game when I came across a weird bug. I have a PlayerAdded event that connects to a function and has been working just fine until I added a new function to my game. After I added the completely unrelated function to my game, the PlayerAdded event stopped working.
Whenever I remove the function call the PlayerAdded event starts working again.
There are no errors whatsoever. I even put a print function (prints ‘test1’ on the line before the PlayerAdded event and a print on the first line in the playerAdded function (prints ‘test2’) that the event connects to.
Here’s an example of what I mean

local function onPlayerAdded()
    print('test2') -- does not print
end
print('test1') --prints
game.Players.PlayerAdded:Connect(onPlayerAdded)

This shows the the script is definetly not halting and is reaching the event listener but is still not connecting to the function.

For clarification I am playtesting the game so my player is being added. I’m not sure why it doesn’t work. :confused: If anyone has any ideas as to why this might be happening, please let me know.

It’s probably because the player has joined before PlayerAdded has been connected which happens sometimes, try adding this to your code.

for _, player in pairs(game.Players:GetPlayers()) do
    coroutine.wrap(onPlayerAdded)(player)
end
1 Like