Players.PlayerAdded:Connect(function(player)
print("player joined")
dataStore2.Combine("DATA", "Sent", "Time") --tick() essentially and later compared
module.GetStats(player)
print("stats should exist")
end)
nothing prints other than print statements immediately before the function, meaning everything runs fine until here, there are also no script errors.
It just doesn’t fire at all, no errors in the output other than an infinite yield possible on DataFolder; but that’s because DataFolder never got added due to the PlayerAdded event never firing and thus the folder not getting created in the first place.
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!