In ServerScriptService I need this sequence, using DataStore:
Load generic game data
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?
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
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.