How to add more data to save without losing the other data with profileService?

Recently I have half mastered the ProfileService, and well I am lost in one thing. I want to add more data that can be saved, however, when adding new data, this data does not exist when starting the game, there are only those that already were previously. An unwanted solution, is to change the GetProfileStore for another, but the data would be lost and I don’t want that, I just want to add another one

--These are the original data:
local saveStructure = {
	Money = 0;
	LogInTimes = 0;
	LogInGiift = 0;
}
--It is this way now:
local saveStructure = {
	Money = 0;
	ToolsWithoutStack = {};
	ToolsWithStack = {};
	LogInTimes = 0;
	LogInGiift = 0;
}
local Players = game:GetService("Players") 
local ProfileService = require(script.ProfileService)
local saveStructure = {
	Money = 0;
	ToolsWithoutStack = {};
	ToolsWithStack = {};
	LogInTimes = 0;
	LogInGiift = 0;
}

local PlayerProfileStore = ProfileService.GetProfileStore("test11", saveStructure)

local cachedProfiles = {}

local function DoSomethingWithALoadedProfile(player, profile)
	local GiftMoney = math.random(100, 270)
	profile.Data.LogInTimes = profile.Data.LogInTimes + 1
	profile.Data.LogInGiift = profile.Data.LogInGiift + 1
	print(player.Name, " has logged in " .. tostring(profile.Data.LogInTimes)..	" time" .. ((profile.Data.LogInTimes > 1) and "s" or ""))
	if profile.Data.LogInGiift >= 2 then
		profile.Data.Money = profile.Data.Money + GiftMoney
		print(player.Name, "has been given a gift of $".. GiftMoney.. ".", "Actualmente tiene $".. profile.Data.Money.. ".")
		profile.Data.LogInGiift = 0
	else
		print(player.Name .. " owns " .. tostring(profile.Data.Money) .. " now!")
	end
end

local function PlayerAdded(player)
	local profile = PlayerProfileStore:LoadProfileAsync("Player_".. player.UserId, "ForceLoad")
	if profile ~= nil then
		profile:ListenToRelease(function()
			cachedProfiles[player] = nil
			player:Kick("Tus datos no han sido cargados. Por favor únase nuevamente")
		end)
		if player:IsDescendantOf(Players) then
			cachedProfiles[player] = profile
			DoSomethingWithALoadedProfile(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick("No se pueden cargar tus datos. Por favor únase nuevamente")
	end
end

for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end

local function PlayerRemoving(player)
	local profile = cachedProfiles[player]
	if profile ~= nil then
		profile:Release()
	end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

function cachedProfiles:Get(player, yield)
	local profile = cachedProfiles[player]
	if yield and not profile then
		repeat task.wait(0.1)
			profile = cachedProfiles[player]
		until profile or (not player.Parent)
	end
	if profile then
		return profile
	end
end

return cachedProfiles
1 Like

It seems you missed out on a crucial piece of code. The Reconcile function! The reconcile function is a function called on the data load right before you use the ListenToRelease connection. It does exactly what you say and adds the new data to the old key. This is what your new code should look like in the player added function. if you want a more in depth explanation take a peak at the API here: API - ProfileService

local function PlayerAdded(player)
	local profile = PlayerProfileStore:LoadProfileAsync("Player_".. player.UserId, "ForceLoad")
	if profile ~= nil then
        profile:Reconcile()
		profile:ListenToRelease(function()
			cachedProfiles[player] = nil
			player:Kick("Tus datos no han sido cargados. Por favor únase nuevamente")
		end)
		if player:IsDescendantOf(Players) then
			cachedProfiles[player] = profile
			DoSomethingWithALoadedProfile(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick("No se pueden cargar tus datos. Por favor únase nuevamente")
	end
end
6 Likes