ProfileService keeps falsely saying Data doesn't exist for a key when it does

I’m using ProfileService for my Game, but whenever I try to retrieve or set a value for a key, Profile Service claims ServerScriptService.PlayerDataHandler:64: Data doesn't exist for key: Music
This is weird considering when I print out the player’s data, it shows the key clearly exists

 {
                    ["Level"] = 1,
                    ["Music"] = false
}  -  Server - PlayerDataHandler:34

I don’t know what I’m doing wrong, since it seems the key exists, but it’s claiming that it doesn’t.

PlayerDataHandler:

local PlayerDataHandler = {}

local ProfileTemplate = {
	Music = true,
	Level = 1
}

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

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

local Profiles = {}

local function playerAdded(plr:Player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..plr.UserId)
	
	if profile then
		profile:AddUserId(plr.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			Profiles[plr] = nil
			plr:Kick()
		end)
		
		if not plr:IsDescendantOf(Players) then
			profile:Release()
		else
			Profiles[plr] = profile
			print(Profiles[plr].Data)
		end
	else
		plr:Kick("Profile Failed to Load, Please Rejoin.")
	end
end

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

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

function PlayerDataHandler:Get(plr:Player,key)
	local profile = getProfile(plr)
	assert(profile.Data[key], string.format("Data doesn't exist for key: %s",key))
	
	return profile.Data[key]
end

function PlayerDataHandler:Set(plr:Player,key,value)
	local profile = getProfile(plr)
	assert(profile.Data[key], string.format("Data doesn't exist for key: %s",key))
	assert(type(profile.Data[key]) == type(value), string.format("Value Type Incorrect"))
	
	profile.Data[key] = value

	return profile.Data[key]
end

function PlayerDataHandler:Update(plr:Player,key,callback)
	local profile = getProfile(plr)
	
	local old = self:Get(plr,key)
	local new = callback(old)
	
	self:set(plr,key,new)
end

return PlayerDataHandler

I would say it has something to with the assert function

Hopefully This helps

   function PlayerDataHandler:Get(plr:Player, key)
    local profile = getProfile(plr)
    if profile.Data[key] == nil then
        error(string.format("Data doesn't exist for key: %s", key))
    end
    return profile.Data[key]
end

function PlayerDataHandler:Set(plr:Player, key, value)
    local profile = getProfile(plr)
    if profile.Data[key] == nil then
        error(string.format("Data doesn't exist for key: %s", key))
    end
    assert(type(profile.Data[key]) == type(value), string.format("Value Type Incorrect for key: %s", key))
    
    profile.Data[key] = value
    return profile.Data[key]
1 Like

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