Save your player data with ProfileService! (DataStore Module)

ProfileService automatically releases profiles when a server is shutting down, so you shouldn’t bother manually connecting :Release() to :BindToClose().

If you’re editing the insides of ProfileService, then I can’t help you there either.

1 Like

I kick the player if the profile is nil. Why is it nil in the first place?

Edit: Also it warns me saying ProfileService doesn’t save dictionaries?

If you’re loading the profile properly, then the only way to get nil is to load the profile in two different places at the same time.

Am I loading the profile wrong, then? This code doesn’t seem wrong to me

local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId, "ForceLoad")
	if profile then
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick("Your profile has been loaded remotely. Please rejoin")
		end)
		if player:IsDescendantOf(Players) then
			Profiles[player] = profile
			PlayerDataLoaded(player)
		else
			profile:Release()
		end
	else
		print(profile)
	end

If you have this code repeated two times in the same game, one of the scripts will receive the profile as nil and perform the kick.

Thank you! However, I have one more question. Can we work with dictionaries in datastores? I put one to keep track of the skins owned by the player but I get a warning "[ProfileService]: DataStore API error [Store:“PlayerData”;Key:“Player_2400337484”] - “104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.”

It can store dictionaries, just not the dictionary you provided - see the troubleshooting page in the official documentation and you’ll sort this out.

2 Likes

DataStores save data with JSON.

This is usually just you not serializing something, Lua tables can sometimes not transform into JSON for some reasons.

JSON only supports dictionaries which only have string as the key, so no other types of keys, it also supports arrays, and the definition of array here is any table that only has indexes from 1-n, no gaps, and no other string keys inside that same array.

JSON also only supports as values: booleans, numbers, strings, and other tables which follow the same rules.
So, things like Color3s, Vector3s, CFrames, and many others need to handled manually by you.

JSON also doesn’t support cyclic-references.

2 Likes

Im only editing the inside of profile.Data but does it still apply? I also don’t connect :Release() to :BindToClose, only to .PlayerRemoving but maybe it also applies.

I fixed it, this is actually what happened: so for profile.Data, I add a DataChanged signal and a _state table that stores the original table. And then, for every table inside profile.Data, I add a KeyChanged signal which caused _state to have a KeyChanged signal when it’s not supposed to, and there is a table inside _state which keeps on repeating that which probably causes the error.

I encountered two errors whilst using your ProfileService for more information see the topic here: [ERROR] ProfileService. Please let me know if there’s a solution. Thank you!

Is it possible to get the user’s data with just the userId? Even if they are not online?

ProfileService:ViewProfileAsync(dataStoreProfileKey)
1 Like

efficient, thanks for the effort. keep up the good job

How many things can I have in a profile? C:

I’m kind of new to ProfileService, but how do you remove or add items to a table using ProfileService?

Profiles have a Data property which is a table that contains all your values. Just edit that table and you’re done.

1 Like

I do know that, but I’m just confused on where you reference it like. Do you do Profile.Data.Tables or something thats what I’m confused on.

local serProfiles = require(module)
local storeTest = serProfiles.GetProfileStore(strName, tabTemplate)
local profileTest = storeTest:LoadProfileAsync(strKey, function() return "ForceLoad" end)

profileTest.Data.Stuff1 = 1
profileTest.Data.Stuff2 = 2
profileTest.Data.Stuff3 = 3
1 Like

But can I edit it too even without them online?