Save your player data with ProfileService! (DataStore Module)

Yes, I am calling the function with the Player’s instance.

You might wanna consider have a :WaitForProfile function.
Get a signal module, then, you can implement it something like this: (pseudo code)

local ON_PROFILE_CACHED = Signal.new()

-- On loading the profile
PROFILES[player] = profile
ON_PROFILE_CACHED:Fire(player, profile)

--

function ProfileHandler:WaitForProfile(player)
    --\\ This function CAN return nil, and it will do so if the player has left.

    do
        local profile = PROFILES[player]
        if profile then
            return profile
        end
    end

    if player.Parent ~= Players then
        return
    end

    while true do
        local playerFromProfile, profile = ON_PROFILE_CACHED:Wait()
        if playerFromProfile == player then
            return profile
        end
        if player.Parent ~= Players then
            return
        end
    end
end
1 Like

Reading the documentation got me confused…

:OverwriteAsync():
Using this method for editing latest player data when the player is in-game can lead to several minutes of lost progress - it should be replaced by [:LoadProfileAsync()]

How would I use :LoadProfileAsync() instead of :OverwriteAsync() when player is active?

  1. How do I know if the player is active?
  2. Wouldn`t that kick the active player by default if profile is stolen?
  3. Wouldn`t loading and manipulating data be slower than Overwriting?

Profile:ClearGlobalUpdates()
Are these updates that would come from the old save or are these currently live ones that will be eliminated? Why would you want it?

What would be the use case for .RobloxMetaData if it`s so limited and ProfileService already has this great feature?

CtrlF “runing” for typo.

You just use it like you would load regular player data - Load, edit, release.

:LoadProfileAsync() will wait for a remote server to do it’s final auto-save and create a profile release signal which would notify developer code not to attempt editing the profile anymore. If there’s no remote server currently using the profile, :LoadProfileAsync() will return a Profile much more instantly.

GlobalUpdates are stored in the profile itself under the same DataStore key, but outside Profile.Data, like metada. Profile:ClearGlobalUpdates() can be used for profile payloads retrieved through :ViewProfileAsync() and :ProfileVersionQuery() to clear GlobalUpdates data in the profile payload. Profile payloads don’t get auto-saved or released - any changes made to them would only be saved to the DataStore if you called :OverwriteAsync().

If you’re not using GlobalUpdates in your game at all, then this method will have no effect to data and can be omitted.

And if you’re actually using GlobalUpdates… Using Profile:ClearGlobalUpdates() when rolling back is not mandatory, in which case a player would recover GlobalUpdates in the exact state they existed in the profile snapshot that was rolled back to.

Yeah. .RobloxMetaData is currently useless and Profile.MetaData.MetaTags, Profile:SetMetaTag(), Profile:GetMetaTag() should be used instead (the former having a 300 character (300 byte) limit, the latter (combined with Profile.Data) having a 4 megabyte limit).

Roblox is promising “querying and indexing based on such metadata will be supported in future releases”, so ProfileService is just being prepared for it.

4 Likes

Wanted to use this module, I have some questions though:

  • Is there a readable way of using serialization? For instance, in DataStore2 the saved data can get serialized, but when reading the data its deserialized.

  • Is there anything I would need to do with BindToClose? Or is it done automatically?

  • For replication, would I need to do:

    profile.Data.Coins = 100
    leaderstats.Coins.Value = 100
    

    Or do I need to use some OnUpdate function?

  • Is it advised to ALWAYS Release profiles and ListenToHopReady() before teleporting?

  • Is it safe to use more than 1 profile at once? (for place specific data)

  • Do you still need to perform this check:

    if profile.Data.Cash == nil then
        profile.Data.Cash = 0
    end
    

    …if you use profile:Reconcile()?

ProfileService discourages serialization, especially when DataStore keys have a limit of 4 megabytes.

Check the module source to answer questions like that. Yes - ProfileService does this automatically.

The official ProfileService replication solution combo is ReplicaService. If it’s too advanced for you, then you’ll have to make up your own code that both updates Profile.Data and replicates to clients.

Not using :ListenToHopReady() could only result in slightly longer load times after teleportation. You’ll have to set up teleportation in your game and test it yourself.

Yes. Also ProfileService is NOT going to work for multi-server access at the same time - one profile will only be loaded on one server at a time.

You don’t have to check this if you’re using :Reconcile().

3 Likes

Thank you for the answers!

Also to clear up some confusion, when I said

I meant, is it safe to load more than 1 profile for a single player. The reason for this is that my game uses multiple places, and each one has a small set of data that is only needed in that place.

I dont know if its worth setting up a different profile per place, or just adding a table to the main profile for each place. (given that Im not hitting the datastore limits anytime soon, my current data table includes everything and uses only 2013 characters)

So is doing something like

profile.Data.Coins = 100
player.leaderstats.Coins.Value = profile.Data.Coins

Considered incorrect usage of ProfileService? Or is this completely fine?

1 Like

You can do both of those things you’re talking about. I would personally not exceed 2 profiles per player, though.

3 Likes

question, I might be missing something but does this support deep copying? like if I remove a variable/key in the defaultstats/template it’ll also get removed in the data?

was trying to remove the ‘What’ variable on the default profile template but it seems to not work so i went ahead and used an module that i found that deep copies everything for me
image

See this, @LucasTutoriaisSaimo have already explained it few posts above, hope this helps!

1 Like

Oh, thanks! I thought it had some hidden stuff that does it for me.

How would I go about listing all the profiles (like all saved)? Since I would like to see everyone’s data and ensure that everything is correct.

Something like this:

local profiles = someMagicalFunction()
for _, profile in pairs(profiles) do
    print(profile.Data)
end

Thousands (or millions, for some games) of player data would be too large of a thing to load all at once. The closest thing to what you’re asking for was only released recently, but it’s purpose is still not for things like finding data of all players:

If you can list out the profile keys, then you can load actual profiles themselves via :LoadProfileAsync() by passing in the profile keys.

2 Likes

Same issue, but I would like to erase all data. Creating a new datastore does not solve the issue, because the old one would still be present - and take up memory.

How do you recommend someone access the player profiles across multiple scripts? Using _G? My data manager is in a module so if I wanted to access the profiles I would have to require the module in that script, should I put the profiles table in _G/shared?

It is best practice to make specific functions to edit data than it is having a public-read variable. However, if you really need it, then you can just define Profiles as a variable within the module like this:

module.Profiles = {}

My DataManager manages all data and will probably be very long, my thought process was that if I require this long module in another script just to use a simple table of profiles that it contains seemed very unnecessary because I would be requiring the whole module when I just needed to access the Profiles table.

Hi, currently running into an issue with ProfileService!

This is my code. In this case, player is Player_-1 and tier is equal to 1.

    print(Profiles)
    local profile = Profiles[player]
    profile.Data.SubTiers[tier] += 1
    print(Profiles)

When I print Profiles the first time, this is the data section that is outputted.
BUG1

When I print it the second time, the value has gone up as desired! The problem is, for some reason all other players in the game are also having the same value replicated, and I’m having a real headache working out why this is replicating, as I have made sure that this code is only being ran once and only for one profile.

BUG2

Any advice would be greatly appreciated :slight_smile:

4 Likes

A) You’ve set the same profile reference to multiple players
B) You’ve set the same member “SubTiers” table reference to multiple profiles - in this case you need to deepcopy before setting a template table to a new profile.

3 Likes

I completely forgot that tables are done by reference by default in lua… xD Thank you so much! This caused me such a headache.