PlayerAdded not firing

when I do

  Players.PlayerAdded:Connect(function(player)
      print("player joined")
  end)

It prints, but when I use it like this :
    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.

everything before the function works,

image

2 Likes

Can you send the whole script? From the first line.

every thing prints until the event ,

image

as you can see that until just before the event fires, all runs well (all statements print, even “player added does not fire” prints)

before that I just define variables, is it necessary still?

I guess you haven’t defined “Players” if its the first line.

Where is your script located? And is it a server or local script?

a server script .

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")

It wont work in studio, due to the fact that the server starts once the player joins,and there is a pile of work before it, so it should work in-game.

10 Likes

@phSalami is most likely correct, your character joins in studio before the event had time to run

1 Like
local event = game:GetService("ReplicatedStorage").event
local err = game:GetService("ReplicatedStorage").error

Errors for me.

because you don’t have the ‘error’ and ‘event’ Remote events in replicated storage, or do you?

I would assume there is no error in the script, correct?

For some reason this actually does print in the Developer Console in game but not in studio, what is the actual reason??

image

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.

3 Likes

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.

25 Likes

wont this run the code for EVERY player in game when each new player is added?

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.

10 Likes

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.

1 Like

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!