ProfileService issues

  1. What do you want to achieve? Keep it simple and clear!
    For profileservice to load profiles without it always ending up nil
  2. What is the issue? Include screenshots / videos if possible!
    When the game is loading profileservice keeps loading before my data module can load, so I added a wait function so the profile can actually load but when profileservice actually loads and my datastore module can continue the profiles still end up nil
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    A repeat wait until the profile was loaded, swapping datastores. I’ve looked for solutions but haven’t found any problems similar to mine.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local DataService = {}

local ProfileTemplate = {
	-- Player Variables
	FirstName = "";
	LastName = "";
	Title = "";
	Race = "Human";
	NameChosen = false;

	-- General Variables
	Rank = "Human";
	Division = "";

	-- Zanpakuto Variables
	SealedForm = "";
	ReleaseCommand = "";
	ReleaseType = "";

	-- Visored Variables
	MaskVariation = "";
}

local ProfileService = require(game.ServerScriptService.Modules.ProfileService)
local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerDataTest2",
	ProfileTemplate
)

local Profiles = {}

local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync(
		"Player_"..player.UserId
	)
	
	if profile ~= nil then
		warn("profile is not nil")
		profile:Reconcile()
		profile:AddUserId(player.UserId)
		
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick("The profile could've been loaded on another Roblox server")
		end)
		
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
			print(profile[player].Data)
		else
			profile:Release()
		end
	else
		player:Kick("The profile couldn't be loaded possibly due to other Roblox servers trying to load this profile at the same time")
	end
end

function DataService:Init()
	for _, player in game.Players:GetPlayers() do
		task.spawn(PlayerAdded, player)
	end
	
	Players.PlayerAdded:Connect(PlayerAdded)
	
	Players.PlayerRemoving:Connect(function(player)
		if Profiles[player] ~= nil then
			Profiles[player]:Release()
		end
	end)
end

task.wait(5)

local function GetProfile(player)
	assert(Profiles[player], string.format("Player %s", player.Name .. " is not assigned a profile"))
	
	return Profiles[player]
end

function DataService:GetDataKey(player, key)
	local profile = GetProfile(player)
	assert(profile.Data[key], string.format("Data does not exist for %s", key))
	
	return profile.Data[key]
end

function DataService:SetDataKey(player, key, value)
	local profile = GetProfile(player)
	assert(profile.Data[key], string.format("Data does not exist for %s", key))
	
	assert(type(profile.Data[key]) == type(value))
	
	profile.Data[key] = value
end

function DataService:UpdateData(player, key, callback)
	local profile = GetProfile(player)
	
	local OldData = self:GetDataKey(player, key)
	local NewData = callback(OldData)
	
	self:SetDataKey(player, key, NewData)
end

return DataService