How to avoid `PlayerAdded` being fired until the initial server load script finishes?

In ServerScriptService I need this sequence, using DataStore:

  1. Load generic game data
  2. Load player’s data

So I have:

Players.PlayerAdded:Connect( function(Player)
    -- ... player's data load... MUST RUN AFTER generic data load
end)

-- ...  generic game data load (MUST RUN FIRST)

However, it seems that Roblox is not waiting for the generic game data load and it is firing PlayerAdded BEFORE the game load is finished.

How to avoid PlayerAdded being fired until the initial server load script finishes?

Is this on a Local script or a Server script?

Please, read the FIRST LINE of the topic… and you can’t use DataStore in localscripts…

1 Like

You can load the data first then connect the event and go through every player to run the PlayerAdded function on already joined players once right after.

-- ...  generic game data load (MUST RUN FIRST)

function PlayerAdded(Player)
    -- ... player's data load... MUST RUN AFTER generic data load
end

Players.PlayerAdded:Connect(PlayerAdded)

for Index, Player in ipairs(Players:GetPlayers()) do
    PlayerAdded(Player) --Going through players once after connecting event just to be sure.
end
1 Like

Put the character added event connection after loading the generic game data.
EDIT: what @BenMactavsin said but put the loop before you put the PlayerAdded scriptConnection so it can’t possibly apply the playerAdded function twice.

1 Like