Server Leaderboard work but global doesn't work

I want to make global leaderboard. Problem is that global leaderboard doesn’t work. I tried to use datastores and watched tutorials but those doesn’t help my code. I asked ChatGpt but even ChatGpt doesn’t have any solutions. My server leaderboard works normally so I think that I can use server leaderboard code and put datastore and use datastore but I am not good at put data into datastore and use it to make working global leaderboard.

Here is code:

    local ds = game:GetService("DataStoreService")
    local data = ds:GetOrderedDataStore("XXXXX")
    ​
    local glpart = script.Parent.Parent.Parent
    local CTRLR = 10
    ​
    local function formatNumber(n)
    	if n >= 1e66 then
    		return string.format("%.1f", n / 1e66):gsub("%.?0+$", "") .. "Uvg"
    	elseif n >= 1e63 then
    		return string.format("%.2f", n / 1e63):gsub("%.?0+$", "") .. "Vg"
    	elseif n >= 1e60 then
    		return string.format("%.2f", n / 1e60):gsub("%.?0+$", "") .. "Nod"
    	elseif n >= 1e57 then
    		return string.format("%.2f", n / 1e57):gsub("%.?0+$", "") .. "Ocd"
    	elseif n >= 1e54 then
    		return string.format("%.2f", n / 1e54):gsub("%.?0+$", "") .. "Spd"
    	elseif n >= 1e51 then
    		return string.format("%.2f", n / 1e51):gsub("%.?0+$", "") .. "Sxd"
    	elseif n >= 1e48 then
    		return string.format("%.2f", n / 1e48):gsub("%.?0+$", "") .. "Qid"
    	elseif n >= 1e45 then
    		return string.format("%.2f", n / 1e45):gsub("%.?0+$", "") .. "Qad"
    	elseif n >= 1e42 then
    		return string.format("%.2f", n / 1e42):gsub("%.?0+$", "") .. "Td"
    	elseif n >= 1e39 then
    		return string.format("%.2f", n / 1e39):gsub("%.?0+$", "") .. "Dd"
    	elseif n >= 1e36 then
    		return string.format("%.2f", n / 1e36):gsub("%.?0+$", "") .. "Ud"
    	elseif n >= 1e33 then
    		return string.format("%.2f", n / 1e33):gsub("%.?0+$", "") .. "Dc"
    	elseif n >= 1e30 then
    		return string.format("%.2f", n / 1e30):gsub("%.?0+$", "") .. "no"
    	elseif n >= 1e27 then
    		return string.format("%.2f", n / 1e27):gsub("%.?0+$", "") .. "Oc"
    	elseif n >= 1e24 then
    		return string.format("%.2f", n / 1e24):gsub("%.?0+$", "") .. "Sp"
    	elseif n >= 1e21 then
    		return string.format("%.2f", n / 1e21):gsub("%.?0+$", "") .. "Sx"
    	elseif n >= 1e18 then
    		return string.format("%.2f", n / 1e18):gsub("%.?0+$", "") .. "Gi"
    	elseif n >= 1e15 then
    		return string.format("%.2f", n / 1e15):gsub("%.?0+$", "") .. "Qa"
    	elseif n >= 1e12 then
    		return string.format("%.2f", n / 1e12):gsub("%.?0+$", "") .. "T"
    	elseif n >= 1e9 then
    		return string.format("%.2f", n / 1e9):gsub("%.?0+$", "") .. "B"
    	elseif n >= 1e6 then
    		return string.format("%.2f", n / 1e6):gsub("%.?0+$", "") .. "M"
    	elseif n >= 1e3 then
    		return string.format("%.2f", n / 1e3):gsub("%.?0+$", "") .. "K"
    	else
    		return tostring(n)
    	end
    end
    ​
    local function refreshLeaderboard()
    	local players = game.Players:GetPlayers()
    	local leaderboardData = {}
    ​
    	for _, player in pairs(players) do
    		local gems = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Gems")
    		if gems then
    			leaderboardData[player.UserId] = formatNumber(gems.Value) -- Käytetään formatNumber-funktiota tässä
    		end
    	end
    ​
    	local success, error = pcall(function()
    		local orderedLeaderboard = {}
    		for userId, gemValue in pairs(leaderboardData) do
    			table.insert(orderedLeaderboard, { key = userId, value = gemValue })
    		end
    ​
    		table.sort(orderedLeaderboard, function(a, b)
    			return a.value > b.value
    		end)
    ​
    		for rank, entry in ipairs(orderedLeaderboard) do
    			local username = game.Players:GetNameFromUserIdAsync(entry.key)
    			local gem = entry.value
    ​
    			local nsapmle = script.Parent.Sample:Clone()
    			nsapmle.Visible = true
    			nsapmle.Parent = glpart.SurfaceGui.plrhandler
    			nsapmle.Name = username
    ​
    			nsapmle.prank.Text = "#"..rank
    			nsapmle.pname.Text = username
    			nsapmle.pgems.Text = gem
    		end
    	end)
    ​
    	if not success then
    		warn("Error updating leaderboard:", error)
    	end
    end
    ​
    while true do
    	for _, frame in pairs(glpart.SurfaceGui.plrhandler:GetChildren()) do
    		if frame.Name ~= "Sample" and frame:IsA("Frame") then
    			frame:Destroy()
    		end
    	end
    	refreshLeaderboard()
    	wait(CTRLR)
    end

more info:

“local glpart = script.Parent.Parent.Parent = main (part)”
“​local data = ds:GetOrderedDataStore(“XXXXX”)” We can use the datastore name as “XXXXX”

PATH:

ServerStats
 -main
   -SurfaceGui
      -plrhandler (ScrollingFrame)
         -GL (script)
         -UIListLayout
         -Sample (Frame)
            -pgems (TextLabel)
            -pname (TextLabel)
            -prank (TextLabel)

I’m really glad if this can be solved! :slight_smile:
Tell me please if I made something wrong in this post. This is my second post…

Ok first off trust me dont use ChatGPT. I started using it to ‘assist’ me in game development, and now I feel like I have been partially brainwashed of my coding skills, especially my problem solving skills. like i need ChatGPT to help me. i need ChatGPT to write code for me. So yea. and onto your actual question, I am having a hard time figuring out what the issue is, but:

  1. Make sure to update the number values on every client, for every player. local scripts might be better for that.
  2. Your format number function is ungodly long, heres a better version:
local notations = {
	{1e3,"K"},
	{1e6,"M"},
	{1e9,"B"},
	{1e12,"T"},
	{1e15,"Qa"},
	{1e18,"Qi"},
	{1e21,"Sx"},
	{1e24,"Sp"},
	{1e27,"Oc"},
	{1e30,"No"}
}

local function formatNumber(number:IntValue)
	local num_string = tostring(number)
	for notation = 1,#notations,1 do
		local min = notations[notation][1]
		local max = notations[math.clamp(notation+1,1,#notations)][1]
		if number >= min and number < max then
			local new_number = math.round((number/min)*10)/10
			num_string = tostring(new_number)
			return num_string..notations[notation][2]
		end
	end
	return num_string
end

And I also recommend Datastore2

Hey,

I tried to make global leaderboard again. Now it shows my name, rank and my gem amount. But when I tested it with 2 test players (Start F7), server leaderboard didn’t show these players stats. It worked before and showed their stats. Also global leaderboard only shows my stats and when I give one of these test players more gems than my account have, my stats disappears and the global board is empty.
It looks like this:

And this is my account’s view:
kuva

Here is Global leaderboard’s code:

local ds = game:GetService("DataStoreService")
local data = ds:GetOrderedDataStore("XXXXX2")

local glpart = script.Parent.Parent.Parent
local CTRLR = 10


local notations = {
	{1e3,"K"},
	{1e6,"M"},
	{1e9,"B"},
	{1e12,"T"},
	{1e15,"Qa"},
	{1e18,"Qi"},
	{1e21,"Sx"},
	{1e24,"Sp"},
	{1e27,"Oc"},
	{1e30,"No"}
}

local function formatNumber(number:IntValue)
	local num_string = tostring(number)
	for notation = 1,#notations,1 do
		local min = notations[notation][1]
		local max = notations[math.clamp(notation+1,1,#notations)][1]
		if number >= min and number < max then
			local new_number = math.round((number/min)*10)/10
			num_string = tostring(new_number)
			return num_string..notations[notation][2]
		end
	end
	return num_string
end



local function refreshLeaderboard()
	local leaderboardData = {}

	
	for i, Player in pairs(game.Players:GetPlayers()) do
		data:SetAsync(Player.UserId, Player.leaderstats.Gems.Value)
	end
	
	local success, Error = pcall(function()
		local dd = data:GetSortedAsync(false, 100)
		local gempage = dd:GetCurrentPage()
		
		for Rank, Saved in pairs(gempage) do
			local username = game.Players:GetNameFromUserIdAsync(Saved.key)
			local gem = formatNumber(Saved.value)

			local nsapmle = script.Parent.Sample:Clone()
			nsapmle.Visible = true
			nsapmle.Parent = glpart.SurfaceGui.plrhandler
			nsapmle.Name = username

			nsapmle.prank.Text = "#"..Rank
			nsapmle.pname.Text = username
			nsapmle.pgems.Text = gem
		end
	end)

end

while true do
	for _, frame in pairs(glpart.SurfaceGui.plrhandler:GetChildren()) do
		if frame.Name ~= "Sample" and frame:IsA("Frame") then
			frame:Destroy()
		end
	end
	refreshLeaderboard()
	wait(CTRLR)
end

Server leaderboard has the same code, what I put before.

I also looked that datastore2 system and it looks little bit messy but if there is no way to fix it, I have to use that datastore2. I will watch tutorials, how to use it.

It’s also almost impossible to test it on the Roblox server because I need to release the update only when I’ve completed the entire update. This Global leaderboard is one part of the update, and I’ve already made changes, so I cannot release it before the update is ready. My game is called ‘Extreme Sauna Simulator’ if you want to check it out.

sorry for bad English
What you can do to test it, is to create a place with just the global leader boards script and everything related to it and test it there.

Ok I’ll try. Thanks. I will tell later, how it works. If you have any solutions beforehand, please don’t hesitate to share them with me.

Okey! It’s now working in test game. Hopefully it works in real game and not only in test game.

This code works if someone needs it:

local ds = game:GetService("DataStoreService")
local data = ds:GetOrderedDataStore("YourOrderedDataStore")

local glpart = script.Parent.Parent.Parent
local CTRLR = 10


local notations = {
	{1e3,"K"},
	{1e6,"M"},
	{1e9,"B"},
	{1e12,"T"},
	{1e15,"Qa"},
	{1e18,"Qi"},
	{1e21,"Sx"},
	{1e24,"Sp"},
	{1e27,"Oc"},
	{1e30,"No"}
}

local function formatNumber(number:IntValue)
	local num_string = tostring(number)
	for notation = 1,#notations,1 do
		local min = notations[notation][1]
		local max = notations[math.clamp(notation+1,1,#notations)][1]
		if number >= min and number < max then
			local new_number = math.round((number/min)*10)/10
			num_string = tostring(new_number)
			return num_string..notations[notation][2]
		end
	end
	return num_string
end



local function refreshLeaderboard()
	local leaderboardData = {}

	
	for i, Player in pairs(game.Players:GetPlayers()) do
		data:SetAsync(Player.UserId, Player.leaderstats.Gems.Value)
	end
	
	local success, Error = pcall(function()
		local dd = data:GetSortedAsync(false, 100)
		local gempage = dd:GetCurrentPage()
		
		for Rank, Saved in pairs(gempage) do
			local username = game.Players:GetNameFromUserIdAsync(Saved.key)
			local gem = formatNumber(Saved.value)

			local nsapmle = script.Parent.Sample:Clone()
			nsapmle.Visible = true
			nsapmle.Parent = glpart.SurfaceGui.plrhandler
			nsapmle.Name = username

			nsapmle.prank.Text = "#"..Rank
			nsapmle.pname.Text = username
			nsapmle.pgems.Text = gem
		end
	end)

end

while true do
	for _, frame in pairs(glpart.SurfaceGui.plrhandler:GetChildren()) do
		if frame.Name ~= "Sample" and frame:IsA("Frame") then
			frame:Destroy()
		end
	end
	refreshLeaderboard()
	wait(CTRLR)
end

If you want to see those leaderboards, here is link for my untitled game. I will remove it when my real game’s leaderboards works 100& correctly: Untitled Game2222 - Roblox
Thanks everyone! :slight_smile:

1 Like

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