Properly using profileservice

I’m trying to use profileservice to save intvalues linked to player but they won’t “save” even though it prints “saved”, because when I start a new playtest session all of my values reset to 0.

local Players = game:GetService("Players")
local cachedProfiles = {}
local ProfileService = require(script.ProfileService)
local saveStructure = {	
	TIX = 0;
	__NOTEXTURES = 0;
}
local PlayerProfileStore = ProfileService.GetProfileStore("PlayerSaveData", saveStructure)

local function PlayerDataLoaded(player)
	local profile = cachedProfiles[player]
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local TIX = Instance.new("IntValue")
	TIX.Name = "TIX"
	TIX.Value = profile.Data.TIX
	TIX.Parent = folder
	local __MISC = Instance.new("Folder")
	__MISC.Name = "__SETTINGS"
	__MISC.Parent = player
	local __NOTEXTURES = Instance.new("IntValue")
	__NOTEXTURES.Name = "__REDUCETEXTURES"
	__NOTEXTURES.Value = profile.Data.__NOTEXTURES
	
	local profile = cachedProfiles[player]

	if profile ~= nil then
		__NOTEXTURES.Value = profile.Data.__NOTEXTURES
		TIX.Value = profile.Data.TIX
	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("Your profile has been loaded remotely. Please rejoin.")
		end)
		
		if player:IsDescendantOf(Players) then
			cachedProfiles[player] = profile
			PlayerDataLoaded(player)
		else
			profile:Release()
		end
	else
		player:Kick("Unable to load saved data. Please rejoin.")
	end
end

for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(function()
		PlayerAdded(player)
	end)
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
	local profile = cachedProfiles[player]
	if profile ~= nil then
		print("saved")
		profile:Release()
	end
end)

return cachedProfiles

It seems like whenever the value of your object value gets changed, it will not be reflected in the profile data, hence the data not being saved.

The point of ProfileService is to have 1 place to keep your data and modify it, without having to worry about saving it. I would recommend changing the stats in the profile data, and then if you want to display them on a leaderboard, then create a function to change stats that also reflects the changes in leaderstats.

I most certainly tried to use a loop too but it wouldn’t work.

task.spawn(function()
		while true do
			local profile = cachedProfiles[player]
			
			if profile ~= nil then
				__NOTEXTURES.Value = profile.Data.__NOTEXTURES
		        TIX.Value = profile.Data.TIX
			else
				break
			end
			
			task.wait(0.1)
		end
	end)

Note that the values in leaderstats are cosmetic only when using ProfileService. When you want to change the data, write to the profile.Data table, not to the leaderstats values.

Also, it’s generally not good practice to have a loop like this to continually update values. Instead, only update the values when needed. You could implement this with a function like this:

local function updatePlayerTix(player, newTixValue)
    local profile = profiles[player]
    profile.Data.TIX = newTixValue
    player.leaderstats.TIX.Value = newTixValue
end

Well i tried that function but my values still wouldn’t save!

local ProfileCache = require(game.ServerScriptService.ProfileCacher)
function updatePlayerTix(player, newTixValue)
	local profile = ProfileCache[player]
	profile.Data.TIX += newTixValue
	player.leaderstats.TIX.Value += newTixValue
end
script.Parent.Touched:Connect(function(__PART)
	local __PLR = game:GetService("Players"):GetPlayerFromCharacter(__PART.Parent)
	if __PLR ~= nil then
		updatePlayerTix(__PLR, 10)
	end
end)

If you’re testing in Studio, can you make sure you have API access enabled for data stores?

Yes it is enabled and my regular datastore used to save (Please note that the regular datastore script is disabled after I added profileservice)