Global Leaderboards sometimes disappearing after a while

So basically I have a global leaderboards script 6 actually each in a folder some are represent with int values some with string to int values essentially the same thing display the highest of ones leader stat but for some of the global leaderboards like it will disappear after a while upon joining or sometimes upon joining wont even display at all even after waiting for their update time so is there a reason why here is one of the scripts but the other 5 are basically the same and sometimes when one disappears another one reappears. And also it works in studios but not in game.

local TempPart = script.Parent
--[[]]--
local SurfaceGui = TempPart.SurfaceGui
local MainFrame = SurfaceGui.Frame
--[[]]--
local Template = script['Template']
--[[]]--
local RecentlySaved = {}
local DataStoreService = game:GetService('DataStoreService')
local GlobalLeaderboardB = DataStoreService:GetOrderedDataStore(":GlobalStrength")
if not game["Run Service"]:IsStudio() then
	GlobalLeaderboardB = DataStoreService:GetOrderedDataStore("Game:GlobalStrength")
end
--[[ SETTINGS ]]--
local CurrencyName = "Strength"
local ListSize = 100
local UpdateEvery = 10
local MinimumRequirement = 1
--[[]]--
local _functions = {}

-- Converts a string to a number, with error handling.
local function safeStringToNumber(value)
	if type(value) == "string" then
		local numberValue = tonumber(value)
		if not numberValue then
			warn("Invalid number string: " .. value)
			return 0
		end
		return numberValue
	end
	return value
end

-- Transform Currency for storage
local function transformCurrency(value)
	return math.log10(value + 1) * (2^63) / 308.254
end

-- Reverse transformed value for display
local function reverseTransformCurrency(value)
	return (10^(value / (2^63) * 308.254)) - 1
end

_functions.returncurrency = function(v)
	local x = 0
	for _, b in pairs(v:GetDescendants()) do
		if (b:IsA("StringValue") or b:IsA("IntValue") or b:IsA("NumberValue")) and (b.Name == CurrencyName) then
			x = safeStringToNumber(b.Value)
			break
		end
	end
	return x
end

_functions.removefromrecentlysaved = function(PlayerName)
	for i, v in pairs(RecentlySaved) do
		if v == PlayerName then
			table.remove(RecentlySaved, i)
			break
		end
	end
end

_functions.autoremoverecentsaved = function(PlayerName)
	task.spawn(function()
		task.wait(15)
		for i, v in pairs(RecentlySaved) do
			if v == PlayerName then
				table.remove(RecentlySaved, i)
				break
			end
		end
	end)
end

_functions.returnplayerlist = function()
	local int = 0
	for _, v in pairs(Players:GetPlayers()) do
		for _, j in pairs(v:GetDescendants()) do
			if j.Name == CurrencyName and safeStringToNumber(j.Value) > MinimumRequirement then
				int += 1
				break
			end
		end
	end
	return int
end

_functions.clear = function()
	for _, v in pairs(MainFrame:GetChildren()) do
		if v:IsA('Frame') then
			v:Destroy()
		end
	end
end

_functions.abbreviate = function(value, idp)
	value = safeStringToNumber(value)
	if not value or value < 1000 then
		return math.floor(value + 0.5)
	end

	local abbreviations = {
		{1e63, "Vg"}, -- Vigintillion
		{1e60, "Nod"}, -- Novemdecillion
		{1e57, "Ocd"}, -- Octodecillion
		{1e54, "Spd"}, -- Septendecillion
		{1e51, "Sxd"}, -- Sexdecillion
		{1e48, "Qui"}, -- Quindecillion
		{1e45, "Qua"}, -- Quattuordecillion
		{1e42, "Td"},  -- Tredecillion
		{1e39, "Dd"},  -- Duodecillion
		{1e36, "Ud"},  -- Undecillion
		{1e33, "Dc"},  -- Decillion
		{1e30, "N"},   -- Nonillion
		{1e27, "Oc"},  -- Octillion
		{1e24, "Sp"},  -- Septillion
		{1e21, "Sx"},  -- Sextillion
		{1e18, "Qi"},  -- Quintillion
		{1e15, "Qa"},  -- Quadrillion
		{1e12, "T"},   -- Trillion
		{1e9, "B"},    -- Billion
		{1e6, "M"},    -- Million
		{1e3, "K"}     -- Thousand
	}

	for _, entry in ipairs(abbreviations) do
		local factor, suffix = unpack(entry)
		if value >= factor then
			local formattedNumber = value / factor
			return string.format(formattedNumber % 1 == 0 and "%d%s" or "%." .. idp .. "f%s", formattedNumber, suffix)
		end
	end

	return string.format("%d", value)
end

-- Player removal handling
Players.PlayerRemoving:Connect(function(Player)
	local PName = Player.Name
	if not table.find(RecentlySaved, Player.Name) then
		table.insert(RecentlySaved, Player.Name)
		local CurrentCurrencyAmount = transformCurrency(_functions.returncurrency(Player))
		local Success, Errormsg = pcall(function()
			GlobalLeaderboardB:SetAsync(Player.UserId, CurrentCurrencyAmount)
		end)
		if not Success then warn(Errormsg) end
		_functions.removefromrecentlysaved(PName)
	end
end)

-- Main loop
while true do
	_functions.clear()
	repeat task.wait() until _functions.returnplayerlist() > 0
	for _, Player in pairs(Players:GetPlayers()) do
		local PName = Player.Name
		if not table.find(RecentlySaved, Player.Name) then
			table.insert(RecentlySaved, Player.Name)
			local CurrentCurrencyAmount = transformCurrency(_functions.returncurrency(Player))
			local Success, Errormsg = pcall(function()
				GlobalLeaderboardB:SetAsync(Player.UserId, CurrentCurrencyAmount)
			end)
			if not Success then warn(Errormsg) end
			_functions.autoremoverecentsaved(PName)
		end
	end
	local Pages = GlobalLeaderboardB:GetSortedAsync(false, ListSize)
	local TopList = Pages:GetCurrentPage()
	for Rank, SavedData in ipairs(TopList) do
		local UserId = SavedData.key
		local TransformedValue = safeStringToNumber(SavedData.value)
		local OriginalValue = reverseTransformCurrency(TransformedValue)
		if OriginalValue >= MinimumRequirement then
			local Template = script['Template']:Clone()
			Template.Parent = MainFrame
			Template.LayoutOrder = Rank
			Template.Score.Text = "" .. _functions.abbreviate(OriginalValue, 1) .. " Strength"
			local function SetRankText(n)
				local A = {}
				A[1] = function()
					Template.Rank.Text = "#1"
					Template.Rank.TextColor3 = Color3.fromRGB(255, 215, 0) -- Gold

				end
				A[2] = function()
					Template.Rank.Text = "#2"
					Template.Rank.TextColor3 = Color3.fromRGB(192, 192, 192) -- Silver

				end
				A[3] = function()
					Template.Rank.Text = "#3"
					Template.Rank.TextColor3 = Color3.fromRGB(205, 127, 50) -- Bronze

				end
				if n <= #A then
					A[n]()
				else
					Template.Rank.Text = "#" .. n
				end
			end
			SetRankText(Rank)
			local User = "Unknown"
			local Success, Errormsg = pcall(function()
				User = Players:GetNameFromUserIdAsync(UserId)
			end)
			Template.Username.Text = User
			Template.Name = User
		end
	end
	task.wait(UpdateEvery)
end
1 Like