Issue with profileService while loading

ServerScriptService.Libs.ProfileService:1738: [ProfileService]: Profile [Store:“_9”;Key:“_9”] is already loaded in this session

It giving error for second plr, when first is joining - everything is ok, but the problem for every next players

local serverScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local mps = game:GetService("MarketplaceService")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("DataStore")

local libs = serverScriptService.Libs
local serverModules = serverScriptService.ServerModules
local Shared = replicatedStorage.Shared
local petsFolder = replicatedStorage.Assets.Pets

local tripleHatchId = 853069493
local autoHatchId = 855072900

local formatNumber = require(Shared.FormatNumber)
local profileService = require(libs.ProfileService)
local profileTemplate = require(script.ProfileTemplate)

local key = "_9"

local profileStore = profileService.GetProfileStore(
	key,
	profileTemplate.Data
)--.Mock

local module = {}
module.Profiles = {}

local function PlayerAdded(player: Player)
	
	local profile = profileStore:LoadProfileAsync(key)
	local config = player:WaitForChild("Config")
	
	--profile:ClearGlobalUpdates()
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local valueFolder = Instance.new("Folder")
	valueFolder.Parent = player.Config
	valueFolder.Name = "Values"

	local petsFolder = Instance.new("Folder")
	petsFolder.Parent = player.Config
	petsFolder.Name = "Pets"
	
	local petsIndex = Instance.new("Folder")
	petsIndex.Parent = player.Config
	petsIndex.Name = "PetIndex"
	
	local redeemCode = Instance.new("Folder")
	redeemCode.Parent = player.Config
	redeemCode.Name = "RedeemCodes"
	
	local trails = Instance.new("Folder")
	trails.Parent = player.Config
	trails.Name = "Trails"

	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		profile:ListenToRelease(function()
			module.Profiles[player] = nil
			player:Kick()
		end)
		if player:IsDescendantOf(players) == true then
			module.Profiles[player] = profile
			
			if mps:UserOwnsGamePassAsync(player.UserId, tripleHatchId) then
				module.Profiles[player].Data.Values.HaveTripleHatch = 1
			end
			if mps:UserOwnsGamePassAsync(player.UserId, autoHatchId) then
				module.Profiles[player].Data.Values.HaveAutoHatch = 1
			end
			
			for i, v in profile.Data.Leaderstats do -- Load leaderstats
				local value = Instance.new("StringValue")
				value.Name = tostring(i)
				value.Value = v
				value.Parent = leaderstats

				runService.Heartbeat:Connect(function()
					if player:IsDescendantOf(players) == true then
						value.Value = module.Profiles[player].Data.Leaderstats[value.Name]
						wait(.1)
					end
				end)
			end
			
			for i,v in profile.Data.RedeemCodes do
				local code = Instance.new("StringValue")
				code.Name = tostring(v)
				code.Parent = config.RedeemCodes
			end
			
			for i,v in profile.Data.Trails do
				local trails = Instance.new("StringValue")
				trails.Name = tostring(v)
				trails.Parent = config.Trails
			end
			
			for i, v in profile.Data.PetsCollected do -- Load petsIndex ( i - num, v - name)
				local petInt = Instance.new("IntValue")
				petInt.Name = tostring(v)
				petInt.Parent = config.PetIndex
			end

			for i, v in profile.Data.Pets do -- Load pets ( i - num, v - name)
				local petInt = Instance.new("IntValue")
				petInt.Name = v["Title"]
				petInt:SetAttribute("Pet_Type", v["Type"])
				petInt.Parent = config.Pets
				
				runService.Heartbeat:Connect(function()
					if player:IsDescendantOf(players) == true then
						petInt.Value = (#config.Pets:GetChildren() + 1)
						wait(.1)
					end
				end)
			end
			
			for i, v in profile.Data.Values do -- Load values
				local value = Instance.new("IntValue")
				value.Name = tostring(i)
				value.Value = v
				value.Parent = valueFolder
				runService.Heartbeat:Connect(function()
					if player:IsDescendantOf(players) == true then
						value.Value = formatNumber.FormatCompact(profile.Data.Values[tostring(i)])
						wait(.1)
					end
				end)
			end
			local char = player.Character or player.CharacterAdded:Wait()
			local hum = char:WaitForChild("Humanoid")
			local punchClone = (replicatedStorage.Assets.Trains.Punch):Clone()
			punchClone.Parent = replicatedStorage.Assets.Trains.Punch.Parent
			hum:EquipTool(punchClone)
			replicatedStorage.Remotes.Events.Pets.LoadPetsAdded:FireClient(player)
		else
			profile:Release()
		end
	else
		player:Kick()
	end
end

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

players.PlayerAdded:Connect(PlayerAdded)

players.PlayerRemoving:Connect(function(player)
	local profile = module.Profiles[player]
	if profile ~= nil then
		profile:Release()
	end
end)

return module

local module = {}

module.Data = {
	Leaderstats = {
		["Foots"] = 10000,
		["Biceps"] = 0,
		["Knuckles"] = 10000,
		["Wins"] = 100
	},
	Values = {
		-- PETS
		["MaxPetsEquipped"] = 3,
		["HaveTripleHatch"] = 0,
		["HaveAutoHatch"] = 0,
		["AutoTriple"] = 0,
		["StorageCap"] = 50,
		-- UTILS
		["EnableTrades"] = 0,
		["Speed"] = 10,
		["RebirthAmt"] = 0,
		["ToggleTrades"] = 1,
	},
	Pets = {},
	PetsCollected = {},
	RedeemCodes = {},
	Trails = {}
}

return module

help pleaz

1 Like

So, if I am reading this correctly, you are saying that every player past the first player who joins the server will cause this error to occur upon joining?

Yes, but i’ve already fixed it. As you can see, i used a key to " LoadProfileAsync ". But when i tried to use “Player”…player.UserId it worked!

Code:
datamngr :point_down:

local serverScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local mps = game:GetService("MarketplaceService")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("DataStore")

local libs = serverScriptService.Libs
local serverModules = serverScriptService.ServerModules
local Shared = replicatedStorage.Shared
local petsFolder = replicatedStorage.Assets.Pets

local tripleHatchId = 853069493
local autoHatchId = 855072900

local formatNumber = require(Shared.FormatNumber)
local profileService = require(libs.ProfileService)

local profileIndex = require(script.ProfileIndex)
local profileTemplate = require(script.ProfileTemplate)


local profileStore = profileService.GetProfileStore(
	profileIndex,
	profileTemplate.Data
)--.Mock

local module = {}
module.Profiles = {}

local function PlayerAdded(player: Player)
	
	local profile = profileStore:LoadProfileAsync("Player"..player.UserId)
	local config = player:WaitForChild("Config")
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local valueFolder = Instance.new("Folder")
	valueFolder.Parent = config
	valueFolder.Name = "Values"

	local petsFolder = Instance.new("Folder")
	petsFolder.Parent = config
	petsFolder.Name = "Pets"

	local petsIndex = Instance.new("Folder")
	petsIndex.Parent = config
	petsIndex.Name = "PetIndex"

	local redeemCode = Instance.new("Folder")
	redeemCode.Parent = config
	redeemCode.Name = "RedeemCodes"

	local trails = Instance.new("Folder")
	trails.Parent = config
	trails.Name = "Trails"

	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		profile:ListenToRelease(function()
			module.Profiles[player] = nil
			player:Kick()
		end)
		if player:IsDescendantOf(players) == true then
			module.Profiles[player] = profile
			
			if mps:UserOwnsGamePassAsync(player.UserId, tripleHatchId) then
				module.Profiles[player].Data.Values.HaveTripleHatch = 1
			end
			if mps:UserOwnsGamePassAsync(player.UserId, autoHatchId) then
				module.Profiles[player].Data.Values.HaveAutoHatch = 1
			end
			
			for i, v in profile.Data.Leaderstats do -- Load leaderstats
				local value = Instance.new("StringValue")
				value.Name = tostring(i)
				value.Value = v
				value.Parent = leaderstats

				runService.Heartbeat:Connect(function()
					if player:IsDescendantOf(players) == true then
						value.Value = module.Profiles[player].Data.Leaderstats[value.Name]
						wait(.1)
					end
				end)
			end
			
			for i,v in profile.Data.RedeemCodes do
				local code = Instance.new("StringValue")
				code.Name = tostring(v)
				code.Parent = config.RedeemCodes
			end
			
			for i,v in profile.Data.Trails do
				local trails = Instance.new("StringValue")
				trails.Name = tostring(v)
				trails.Parent = config.Trails
			end
			
			for i, v in profile.Data.PetsCollected do -- Load petsIndex ( i - num, v - name)
				local petInt = Instance.new("IntValue")
				petInt.Name = tostring(v)
				petInt.Parent = config.PetIndex
			end

			for i, v in profile.Data.Pets do -- Load pets ( i - num, v - name)
				local petInt = Instance.new("IntValue")
				petInt.Name = v["Title"]
				petInt:SetAttribute("Pet_Type", v["Type"])
				petInt.Parent = config.Pets
				
				runService.Heartbeat:Connect(function()
					if player:IsDescendantOf(players) == true then
						petInt.Value = (#config.Pets:GetChildren() + 1)
						wait(.1)
					end
				end)
			end
			
			for i, v in profile.Data.Values do -- Load values
				local value = Instance.new("IntValue")
				value.Name = tostring(i)
				value.Value = v
				value.Parent = valueFolder
				runService.Heartbeat:Connect(function()
					if player:IsDescendantOf(players) == true then
						value.Value = formatNumber.FormatCompact(profile.Data.Values[tostring(i)])
						wait(.1)
					end
				end)
			end
			local char = player.Character or player.CharacterAdded:Wait()
			local hum = char:WaitForChild("Humanoid")
			local punchClone = (replicatedStorage.Assets.Trains.Punch):Clone()
			punchClone.Parent = replicatedStorage.Assets.Trains.Punch.Parent
			hum:EquipTool(punchClone)
			replicatedStorage.Remotes.Events.Pets.LoadPetsAdded:FireClient(player)
		else
			profile:Release()
		end
	else
		player:Kick()
	end
end

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

players.PlayerAdded:Connect(PlayerAdded)

players.PlayerRemoving:Connect(function(player)
	local profile = module.Profiles[player]
	if profile ~= nil then
		profile:Release()
	end
	if workspace.Player_Pets:FindFirstChild(player.Name) then
		workspace.Player_Pets:FindFirstChild(player.Name):Destroy()
	end
end)

return module

profile index :point_down:

return "MyDataStore"

also dont forget to parent profileTemplate and profileIndex to dataManager

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.