Profile Service Accessing Inquiry

So I’m making a global leaderboard using Profile Service, but I don’t really know how to access everyone’s profiles. So far my script only accesses the servers players and makes a leaderboard that way. When the player in the server leaves the leaderboard gets readjusted. However I want to make a global leaderboard using profile service and I don’t know how to do that.

My leaderboard Module Script:

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

local leaderboard = {}


local FRAME_SPACING = 0.5 

leaderboard.sort = function()
	local Coins = {}
	
	print(Manager.Profiles)
	for player, profile in pairs(Manager.Profiles) do
		if profile and profile.Data and type(profile.Data.Coins) == "number" then
			table.insert(Coins, { UserId = player.UserId, Value = profile.Data.Coins })
		end
	end

	table.sort(Coins, function(a, b) return a.Value > b.Value end)

	local playerFrame = game.Workspace.LeaderBoardCoins.SurfaceGui.ScrollingFrame.PlayerFrame
	for _, v in pairs(playerFrame:GetChildren()) do
		v:Destroy()
	end

	for rank, data in ipairs(Coins) do
		local userId = data.UserId
		local playerName = "Unknown"
		local player = Players:GetPlayerByUserId(userId)
		if player then
			playerName = player.Name
		else
			local success, result = pcall(function()
				return Players:GetNameFromUserIdAsync(userId)
			end)
			if success then
				playerName = result
			else
				warn("Failed to retrieve player name for userId:", userId)
			end
		end

		print("Rank:", rank, "UserId:", userId, "PlayerName:", playerName, "Coins:", data.Value) 

		local newrank = game.ReplicatedStorage.LeaderboardFrame:Clone()
		newrank.Ranking.Text = tostring(rank)
		newrank.Players.Text = playerName
		newrank.Amount.Text = tostring(data.Value)

		local yPos = (rank - 1) * FRAME_SPACING
		newrank.Position = UDim2.new(0, 0, yPos, 0)

		newrank.Parent = playerFrame
	end
end

return leaderboard

DataStore Script:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Template = require(ServerScriptService.PlayerData.Template)
local Manager = require(ServerScriptService.PlayerData.Manager)
local ProfileService = require(ServerScriptService.Libs.ProfileService)

local ProfileStore = ProfileService.GetProfileStore("Production", Template)
local Remotes = ReplicatedStorage.Remotes

local KICK_MESSAGE = "There was an issue loading your data, try again later"

local function CreateLeaderstats(player: Player)
	local profile = Manager.Profiles[player]
	if not profile then return end

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Coins = Instance.new("NumberValue", leaderstats)
	Coins.Name = "Coins"

	local rebirths = Instance.new("NumberValue", leaderstats)
	rebirths.Name = "Rebirths"
	rebirths.Value = profile.Data.Rebirths

	if type(profile.Data.Coins) == "number" then
		Coins.Value = profile.Data.Coins
	else
		warn("Coins value is not a number for player:", player.Name, "Value:", profile.Data.Coins)
		Coins.Value = 0
	end

	Remotes.UpdateClick:FireClient(player, Coins.Value)
end



local function LoadProfile(player: Player)
	local profile = ProfileStore:LoadProfileAsync("Player_".. player.UserId)
	if not profile then
		player:Kick(KICK_MESSAGE)
		return
	end

	profile:AddUserId(player.UserId)
	profile:Reconcile()
	profile:ListenToRelease(function()
		Manager.Profiles[player] = nil
		player:Kick(KICK_MESSAGE)
	end)

	if player:IsDescendantOf(Players) then
		Manager.Profiles[player] = profile
		CreateLeaderstats(player)
	else
		profile:Release()
	end
end


Players.PlayerAdded:Connect(LoadProfile)

Players.PlayerRemoving:Connect(function(player)
	local profile = Manager.Profiles[player]
	if profile then
		profile:Release()
	end
end)
1 Like

I’m looking, but I don’t see any major flaws or anything standing out. I recommend using module scripts to handle data, but you are doing that. Can you explain a bit more in detail what is wrong or what you’re trying to achieve?

hows you output log looking…

Yeah well it works, but the problem is im trying to create a GLOBAL leaderboard. This script creates a leaderboard. My problem is I have no Idea how to access everyone’s profiles globally using profile service. I just know how to do so locally. When I print “Manager.Profiles” I only get my profiles as I am the only one in the server. However when I am in a different server with people I get all our profiles. But I want to be able to get all our profiles regardless of the server. I want to globally access all data profiles in my game.

1 Like

I am still a bit confused; I barely understand what you are trying to achieve.

When you say this I imagine you are asking how to make the data accessible via all servers
which you could do using messaging Service - which lets servers communicate with each other

I personally would have just made a datastore and if the player is high to some degree to anyone on the leaderboard I’ d adjust accordingly

You mention doing it locally I assume you meant retrieving the data from the server via remote function.

Towards the end you seem as if you want to list out all data profiles ever made in your game. I will just browse ProfileService API to see If its possible to do that. I doubt it…

If you want to make a Global leaderboard you just need save a table of the TOP whatever 10-20 in a table with there Amount there high in as well as there ID to retrieve that data that way you can access it regardless by calling on there profile using the ID’s on the GLOBAL leaderboard

Yeah thats the thing I have no Idea how to retrieve the data of the top whatever 10-20 there is. I’m not too familiar with Profile Service API so I don’t even know if its possible or not. If so, how would I retrieve the data of the top 10-20? So far the scripts I have gathers the top 10-20 of the people in THE SERVER not the whole game.

Okay Now I understand you want a global leaderboard, then all you gotta do is just make it :person_shrugging:

By make-it I mean make a datastore for the TOP Whatever it is you want

that would be checking at intervals at all players on the server functions
LeaderBoard.Check()
By looping thru the current Global Leaderboard
and Looping the players
This function will check if the player is on it or can be placed on it
If Player can be added
Leaderboard.Add_Player()
This function will find the placement for the player then add them to that slot
while saving who was in the spot or under and moving that person below them

Currently I think that’s all you would need starting off atleast

The thing is, the leaderboard script I sent also accesses the data of people in the server. So it makes a leaderboard of the people in the server. Not a global leaderboard having the true top 10 or 20. I have no Idea how to grab the data/profiles of all my players rather than the ones just in a server.

I was letting you know what is needed. How much experience do you have scripting? I could type out a lengthy example of what the code would look like, but then how would you grow?

I am aware of the scripts you sent the thing is, you need to create a new script or new code maybe a new module called ‘GlobalLeaderstats’ that will have the functions I described to handle the new datastore for… global leaderboard

It would not be possible to do anything other than grab the current players on the server check if they fit the requirements to be a TOP leader board member then adding them to the leaderboard via a new datastore (doesn’t even have to be profile service since it wouldn’t be a player but just a data table you access not related to a player)

aswell as adding all the logic and conditions to make sure the leaderboard is fixed properly each addition

So your suggesting I create a new data store to save purely the leaderstats. Then use roblox’s ordered datastores to globally order the leaderboard as such? Thats what I did for my old game using roblox datastore. I just didnt know how to do it with profile service api as i am new to it. As for my scripting experience I’ve scripted for around 1 - 2 years so far.