Profileservice data returning as nil

Hello, i am new to profileservice. I am trying to learn how to use it for future creations but I have a problem with it. The profile data, it just returns as nil and doesnt return any of the profile data. Any idea on what i did wrong?

Players = game:GetService("Players")

local ProfileService = require(game.ReplicatedStorage.ProfileService)

local ProfileStore = ProfileService.GetProfileStore(
	"Player",
	{
		money = 0;
	}
)

local Profiles = {}

local function onPlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync(
		"Player_" .. player.UserId,
		"ForceLoad"
	)
	if profile then
		print("player added")
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)
		
		if player:IsDescendantOf(Players) then
			Profiles[player] = profile
			print("isdesc of")
		else
			profile:Release()
		end
	else
		player:Kick("no profile detected")
	end
end

local function onPlayerRemoving(player)
	local profile = Profiles[player]
	if profile then
		profile:Release()
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)



local DataManager = {}



function DataManager:Get(player)
	local profile = Profiles[player]
	if profile[player] then
		print("profile data")
		return profile.Data
	end
end


return DataManager

Always good to debug your code whenever you’re having an issue but there’s no errors going out into your console. Throw some prints or breakpoints around and take some time to review your code as well. I spotted the issue pretty quickly:

if profile[player] then
	print("profile data")
	return profile.Data
end

You’re trying to index the Profile object with the Player instance as a key but profiles don’t have a Player instance key in them. I think you meant to check if the profile variable alone exists.

if profile then
    print("profile data")
    return profile.Data
end

Word of advice as well: since you have an onPlayerAdded function, you should be running that on all players that exist in the server. GetProfileStore is a yielding function that can delay the time it takes to connect the function to the event.

-- Your Players service declaration isn't local in the code example,
-- make sure to fix that as well.
for _, player in Players:GetPlayers() do
    onPlayerAdded(player)
end
2 Likes

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