game.Players.PlayerAdded not working

What is the error message displayed?

There is no error it just doesn’t continue.

It is like there isn’t enough time between when the playerAdded and it firing the event

Could you give us more information on what the Discord.Post is supposed to do? Or in fact, what the whole script is supposed to do. Since we don’t know what the Player script does.

local newPlayer = Player.new(player)

players[player] = newPlayer

warn(string.format("%s[%d]: Data has loaded.", player.Name, player.UserId))

end)

A little confused here too, what is the objective of this script?

1 Like

This is a server script loading all modules. Player.new() loads all player data from its module.

Discord.Post() posts data to a webhook.

However I don’t want the function to fire for everyone.

You won’t be. The reason why this issue is occurring is because the time it takes for your modules to be required and the other code to run, your player instance would be already in the game before the PlayerAdded event has had the chance to connect. The reason why you loop through all players is because in case that happens. This won’t loop through all players every time someone joins, it’s only grabbing all players at the time of that server script running, which will only happen once and will usually only have one player.

game.Players.PlayerAdded will fire for everyone however.

It’s also highly suggested that you queue and buffer your use of Discord webhooks if you’re also going to log Chat’s through it as your webhook could be deleted for not being used in the way that Discord seems fit.

If you’re testing in studio, you’ll need to do both game.Players.PlayerAdded and for i,v in players:GetPlayers() as Studio has yet to bother delaying the loading of the player instance before all your server scripts are loaded so that on random occasions, your script probably didn’t even run yet and the player got added already.

the thing is it was working absolutely fine yesterday

Are we talking ingame as in pressing play through the Roblox website, or play testing through Studio?

Yes, it’s quite random.

Play testing through studio. (30characters)

Then your best bet is to move things into a single function and do both PlayerAdded and check for existing players…

As again, Studio simply runs the server and the client at nearly the same time, so some server scripts may never get the PlayerAdded event as the player is already added before the server script was executed to listen for new players.

And yes, that still occurs at random, sometimes it may work just fine, sometimes it just doesn’t fire. It’ll be up for Roblox to do an update to Studio to actually check to make sure the server is finished loading before running the client.

If you don’t want to test this way, use the other Studio feature in Play. Start a server, then start a client manually.

function PlayerAdded(plr)
    --[[do player things]]
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for i,v in next,game.Players:GetPlayers() do
    PlayerAdded(v)
end

The script above simply will fire the PlayerAdded function when a new player is added AND checks the existing players ingame to have them fire PlayerAdded so that you account for everybody ingame.

11 Likes

But wont this fire the PlayerAdded function for everyone???

2 Likes

Yes? Isn’t that what you want your script to do? You’re saying that it’s not working and that you’re testing it in studio…

Studio sometimes run the client faster than the server is even done yet so it doesn’t fire PlayerAdded because the player is already added.

You didn’t say anything about how you’re using this script and the use of require for modules also shows that you’re planning to have this as a server script ingame.

The script I’ve provided, it’ll fire PlayerAdded for new players that are joining, and then as a precaution in case the script didn’t run fast enough to run the event for the first player joining the game, it’ll also run PlayerAdded for players that are in the game at the time of the script being executed which is usually nobody in a real Roblox scenario, but in a Roblox Studio Play Test sometimes could have the player added before the server was done yet…

But I am saying when the PlayerAdded function fires it gets the individual player’s data.

Ok? So what’s the problem again?

You’re saying that game.Players.PlayerAdded isn’t working…

and again, that you’re testing in studio which I’ve repeated again, sometimes the PlayerAdded event doesn’t fire in Test in Studio unless you use the other Start Server then add players after.

Have you tested in a Roblox game to see if it runs perfectly fine? Try checking developer console by pressing F9 or typing /console into chat.

Yes I have tried in game and it works fine.

I understand why it is not working because of your explanation. However is there not a way to wait until the server has loaded

Then the issue you’re having is that the Studio server started but didn’t run all of your server scripts yet…

  1. You start a Play Test in Studio
  2. Studio creates the server.
  3. The barebones of the Studio server is up and running.
  4. Studio starts adding the player.
  5. The player got added.
  6. Your server script is finally done running and attached PlayerAdded.

This is why in Play Test in Studio, you’ll need to add a postcheck after adding PlayerAdded to see if any players are still in your game before the script was even able to fully run.