Profile Service Keeps Failing To Load Data

I don’t know much about profile service and have quite minimal knowledge on it, as a scripter it’s one of my weak areas. However, I got a script off of someone that I commissioned for a system and I noticed that most the times that I join the game, I get kicked with the messsage "Data issue, try again shortly. If issue persists, contact us!". This keeps happening, over and over…

I even tried to add in a retry, so that it retries to load the data 3 times before kicking and that didn’t work either. The script is below.

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local Template = require(script.Parent.Template)
local ProfileService = require(script.Parent.Parent.Libs.ProfileService)
local Manager = require(script.Parent.Manager)
local PetManager = require(script.Parent.Manager.PetManager)

local ProfileStore = ProfileService.GetProfileStore("Production", Template)

local MAX_RETRIES = 3

local function Log(message)
	print("[" .. os.date("%X") .. "] " .. message)
end

local function LoadPets(Player)
	local Busy = Instance.new("BoolValue", Player)
	Busy.Name = "Busy"

	local Pets = Instance.new("Folder", Player)
	Pets.Name = "Pets"

	local success, result = pcall(function()
		PetManager:EquipPets(Player)
	end)

	if not success then
		Log("Error loading pets for " .. Player.Name .. ": " .. tostring(result))
	end	

	Log("Pets Data Loaded for " .. Player.Name .. " ✅")
end

local function GiveLeaderstats(Player)
	local profile = Manager.Profiles[Player]
	if not profile then return end

	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"

	local Gems = Instance.new("IntValue", leaderstats)
	Gems.Name = "Gems"
	Gems.Value = 0

	local Wins = Instance.new("IntValue", leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0

	LoadPets(Player)
end

local function PlayerAdded(player: Player)
	Log("Player added: " .. player.Name)

	local retries = 0
	local profile = nil

	while retries < MAX_RETRIES and not profile do
		local success, result = pcall(function()
			profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
		end)

		if success and profile then
			break
		else
			Log("Failed to load profile for " .. player.Name .. " (attempt " .. (retries + 1) .. "): " .. tostring(result))
			retries = retries + 1
			task.wait(2)
		end
	end

	if not profile then
		Log("Failed to load profile for " .. player.Name .. " after " .. MAX_RETRIES .. " attempts")
		player:Kick("Data issue, try again shortly. If issue persists, contact us!")
		print(player, "was kicked as a result of a data issue.")
		return
	end

	profile:AddUserId(player.UserId)
	profile:Reconcile()
	profile:ListenToRelease(function()
		Manager.Profiles[player] = nil
		player:Kick("Data issue, try again shortly. If issue persists, contact us!")
		print(player, "was kicked as a result of a data issue.")
	end)

	if player:IsDescendantOf(Players) then
		Manager.Profiles[player] = profile
		GiveLeaderstats(player)
	else
		profile:Release()
	end
end

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

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player: Player)
	local profile = Manager.Profiles[player]
	if profile then
		profile:Release()
	end
end)

This is a really crucial bug that I’m struggling with, any advice or pointers as to what is causing me to get kicked everytime would be appreciated. Thanks! :smile:

Try setting the profile to nil on release: profile = nil or Manager.Profiles[player] = nil (not sure thought).

Didn’t seem to work, thanks for the suggestion though. Any other ideas?

Log("Failed to load profile for " .. player.Name .. " (attempt " .. (retries + 1) .. "): " .. tostring(result))

What does this print out? I’m curious to know what result is.