I said every print statement prints, I have defined players earlier :
print("loaded1")
local Players = game:GetService("Players")
local TextService = game:GetService("TextService")
local event = game:GetService("ReplicatedStorage").event
local err = game:GetService("ReplicatedStorage").error
local module = require(script.Parent.startupFunctions)
local dataf = require(script.Parent.dataFunctions)
dataStore2 = require(script.Parent.DataStore2)
print("loaded2")
local api = require(script.Parent.TrelloAPI)
local boardid = api:GetBoardID("Bounty Reference")
local listid = api:GetListID("Bug Reports", boardid)
print("loaded3")
canReport = nil
print("player added does not fire")
Players.PlayerAdded:Connect(function(player)
print("player joined")
Do you see how when you try to join a game (not in studio) it takes a few time (if its a new server) because its loading all scripts, but in studio it loads the character AND load the scripts at the same time, so if there is alot of code before the playeradded function, it will take time before detecting the event.
Yeah, phSalami is correct here. Studio won’t fire PlayerAdded immediately.
My rule of thumb here, which has also fundamentally changed how I structure my code, is that all your functions should be local variables and you do connections at the end of a script. This way, not only do you avoid issues in Studio, but you gain other benefits like neatness (?).
I will always write PlayerAdded code, regardless of what it’s for, in the following manner:
local function playerAdded(player)
end
Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
This is the normal connection expected to make a function run when the player connects to the server but it will also force the function to run for players already in the server that didn’t trigger PlayerAdded.
No, because I don’t iterate through the players in a PlayerAdded function. If the for loop was in the PlayerAdded function, then yes every time a player joins it would also run the function on everyone.
The point of doing it this way is so that you can have your function listen to PlayerAdded as expected, where the function fires for every new player. However, if your code yields or you are testing in Studio, it is possible that a client connects to the server before a PlayerAdded function has the chance to be connected, thus it won’t fire for that player. That’s where the for loop comes into play.
After our function is listening to PlayerAdded, we’re now accounting for new players but not those who joined before our script made the PlayerAdded connection. The for loop runs through players already in the server and then calls it on them so that existing players will be set up as well with anything needed.
Example: leaderstats. If your script yields at the top of the script, it’s possible some players will not get leaderstats created from them because the PlayerAdded connection happens later. So after making the connection for new players, we run the function on existing players so they also get leaderstats, otherwise they wouldn’t have them.
I have also had issues with this in Studio when I use setfenv(), as I am aware that it makes lua quite a bit slower. However, it seems to work in-game.
Hello! Sorry for bringing this topic back, but! I really need this fixed and can’t access Bug Reports. A while ago this was working, but not with it not working it forces the majority of the developer testing in our game to the actual game which is very inefficient! I’m hoping this can be fixed!
How would I add the players character to this function? I tried but I couldn’t figure out a way to have it work even when the player reset their character.
You don’t because the engine passes the player when it internally fires PlayerAdded. This is also only for when the player connects to the experience server, not for resetting - if you need something to run when a player has a character newly spawned, use Player.CharacterAdded.
I can’t really use player.characteradded when players.playeradded isn’t working in studio correctly, I’m trying to connect it to the function that fires after the player has been added.
I’m not too sure what you mean, but I know that this is a result of you not using or understanding PlayerAdded or what I’m saying, not that PlayerAdded doesn’t work in Studio - the latter part is why the line to iterate over players already in the server exists.
It would probably help to show you exactly what’s the issue. May you supply a code sample and explain what the problem is?
Apologies for the small bit of hostility on the first response, but this is a raw idea of what I’m trying to do:
local function PlayerAdded(Player)
Player.CharacterAdded:Connect(function()
print("Test")
end)
end
for _, player in ipairs(Players:GetPlayers()) do
PlayerAdded(player)
end
However “test” never prints out and I can’t figure out a way to make it work unfortunately.
I know It looks a bit dumb but I’ve never really tried working with a system like this.
If you figure it out, then it’s good to reply with the conclusion you arrived at so others can reference your answer rather than just ending it off at “I figured it out”. In any case, for anyone else, it’s as I said before - this is a problem with the code, not PlayerAdded.
Characters similarly need to be checked for existing or new ones, especially in a deferred environment (and in turn, almost anything that has a matching GetX/property for an Added event):
local function onPlayerAdded(player)
local function onCharacterAdded(character)
end
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in Players:GetPlayers() do
onPlayerAdded(player)
end