Profile does not exist Profile Service

Hello, i have been following a tutorial about profile service and When i tried to call one of the tables values in it, this popped up in the console

here is the PlayerDataHandler script

local PlayerDataHandler = require(game.ServerScriptService.PlayerDataHandler)

game.Players.PlayerAdded:Connect(function(player)
	
	local spells = PlayerDataHandler:Get(player, "Spells")
	--if it's an array
	if table.find(spells, "Fireball") then
		print("The fireball exists!")
	end
	--if it's a dictionary and Fireball is the key
	if spells["Fireball"] ~= nil then
		print(spells["Fireball"])
	end
end)

and here is a script where i call the table

local PlayerDataHandler = {}

local dataTemplate = {
	Money = 0,
	Spells = {
		"Fireball"
	}
}

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

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerProfile",
	dataTemplate
)

local Profiles = {}

local function playerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	
	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)
		
		if not player:IsDescendantOf(Players) then
			profile:Release()
		else
			Profiles[player] = profile
			

		end
	else
		player:Kick()
	end
end

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

end

local function getProfile(player)
	assert(Profiles[player], string.format("Profile does not exist for %s", player.UserId))
	
	return Profiles[player]
end

--Getter

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

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

function PlayerDataHandler:Update(player, key, callback)
	local profile = getProfile(player)
	
	local oldData = self:Get(player, key)
	local newData = callback(oldData)
	
	self:Set(player, key, newData)
end

return PlayerDataHandler
4 Likes

I suspect the value is being called before its even loaded in.

local PlayerDataHandler = require(game.ServerScriptService.PlayerDataHandler)

game.Players.PlayerAdded:Connect(function(player)
	task.wait() -- give time for data to load
	local spells = PlayerDataHandler:Get(player, "Spells")
	--if it's an array
	if table.find(spells, "Fireball") then
		print("The fireball exists!")
	end
	--if it's a dictionary and Fireball is the key
	if spells["Fireball"] ~= nil then
		print(spells["Fireball"])
	end
end)

Hope this helps.

1 Like