Script runs before player data has loaded

I am using the ProfileStore module and would like to know if there is a function to wait for data to load.

I’m using game.Players.PlayerAdded in my server script to detect when a player loads, but this leads to an error since the player’s data isn’t available yet. I’m currently using task.wait(2) as a workaround, but are there any other methods?

What type of error do you get when you try to get the data that has not been loaded yet? If it is a nil error you could every second after a player joins check if the data is or isn’t nil and break out of the loop when it isn’t

I am getting a attempted to call a nil value error.

Call it in a protected call and check if it errors, if so run again until it doesn’t

Most people make middleware to handle things like this. You might have seen or heard modules called “ProfileManager” before. The way my team handles this is by using a custom RBXScriptSignal implementation called LemonSignal, authored by Data-Oriented-House. We have it so our middleware loads in profiles as players connect to the server. When the profiles load, we cache them, then fire the custom event with the profile and the profile’s owner. The custom event is called “ProfileReady”. When the codebase requests a player’s profile, it does one of two things:

  1. Check if the profile is cached. If so, return the profile
  2. If the profile is not cached, use a repeat-until loop with Signal:Wait until the specific player and their profile is spoken of, then return the profile

This is an alternative method to busy-waiting which helps prevent unnecessary thread activity. Busy-waiting is still an option for you though, which involves some pretty basic code:

function ProfileManager.getProfile(player: Player)
    while not profiles[player] do
        task.wait()
    end

    return profiles[player]
end