Why won't my leaderstats for ProfileService be created?

Hello!

I am currently working on putting profile service into my game. Whenever I join the game my leaderstats won’t show. I looked at the output and there are no errors showing. API services are on and it prints in the output that the data will save, even though there is no leaderstats in. What could I do?

Here are all my scripts I am using:

Data Manager:

local dataManager = {}
dataManager.Profiles = {}

local players = game:GetService("Players")
local serverscriptservice = game:GetService("ServerScriptService")
local modules = serverscriptservice:WaitForChild("leaderboardModules")

local profileservice = require(modules:WaitForChild("ProfileService"))
local template = require(script:WaitForChild("Template"))
local leaderstats = require(script:WaitForChild("Leaderstats"))

local dataKey = "_DataStore"
local profilestore = profileservice.GetProfileStore(dataKey, template)

local function playerAdded(player : Player)
	local profile = profilestore:LoadProfileAsync("Player_"..player.UserId)
	
	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			dataManager.Profiles[player] = nil
			player:Kick("Please rejoin the game, your data did not load.")
		end)
		
		if player:IsAncestorOf(players) then
			dataManager.Profiles[player] = profile
			leaderstats:Create(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick("Please rejoin the game, your data did not load.")
	end
end

local function playerRemoving(player: Player)
	local profile = dataManager.Profiles[player]
	
	if profile ~= nil then
		profile:Release()
	end
end

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

players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerRemoving)

return dataManager

Leaderstats Script:

local module = {}

function module:Create(player: Player, profile)
	print("Hey!")
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = profile.Data.Coins
	Coins.Parent = leaderstats
end

return module

Template Script:

local template = {
	Coins = 0,
}

return template

Player Data Script:

require(script.Parent:WaitForChild("leaderboardModules"):WaitForChild("DataManager"))

Thank you so much for your time,

papahetfan

1 Like

The easy way to remember the difference between ancestors and descendants are that descendants go down in the hierarchy whereas ancestors go up. In most cases this is observable in the Explorer window.

If you run print(player:IsAncestorOf(players)) then you’ll observe that it prints false, which means it will never create the leaderstats. Change it to player:IsDescendantOf(players) and you’ll see that it’s true, and your leaderstats will be made.

Useful resources:
IsAncestorOf - Roblox Creator Documentation
IsDescendantOf - Roblox Creator Documentation

You’re using player:IsAncestorOf(players) instead of :IsDescendantOf(), which means that the code inside will never run since players are descendants of the Player service, not ancestors.