Abbreviating leaderboard cash number

Basically i wanna abreviate the numbers on the leaderboard, but don’t know how to.
So i have the global leaderboard script, but when i have like 50K cash it displays like 50000, and i wanna display it as 50K, and i tried to find a script that could do that, but it didn’t work on the leaderboard.
Here is the script :

-- [ SETTINGS ] --

local StatsName = "Cash"
local MaxItems = 100
local MinValueDisplay = 1
local MaxValueDisplay = 10 ^ 15
local UpdateEvery = 60

-- [ END SETTINGS ] --


local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. StatsName)
local SurfaceGui = script.Parent
local Sample = script.Sample
local List = SurfaceGui.Frame.List
local ItemsFrame = List.ListContent.Items

local function GetItems()
	local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
	local TopPage = Data:GetCurrentPage()
	
	ItemsFrame.Nothing.Visible = #TopPage == 0 and true or false
	
	for i, v in ipairs(TopPage) do
		local UserId = v.key
		local Value = v.value
		local Username = "[Not Available]"
		local Color = Color3.fromRGB(38, 50, 56)
		
		local Success, Error = pcall(function()
		 	Username = game.Players:GetNameFromUserIdAsync(UserId)
		end)
		
		if i == 1 then
			Color = Color3.fromRGB(255, 215, 0)
		elseif i == 2 then
			Color = Color3.fromRGB(192, 192, 192)
		elseif i == 3 then
			Color = Color3.fromRGB(205, 127, 50)
		end
		
		local Item = Sample:Clone()
		Item.Name = Username
		Item.LayoutOrder = i
		Item.Values.Number.TextColor3 = Color
		Item.Values.Number.Text = i
		Item.Values.Username.Text = Username
		Item.Values.Value.Text = Value
		Item.Parent = ItemsFrame
	end
end

while true do
	for i, v in pairs(game.Players:GetPlayers()) do
		local Stats = v.leaderstats:WaitForChild(StatsName).Value
		if Stats then
			pcall(function()
				DataStore:UpdateAsync(v.UserId, function(Value)
					return tonumber(Stats)
				end)
			end)
		end
	end
	
	for i, v in pairs(ItemsFrame:GetChildren()) do
		if v:IsA("ImageLabel") then
			v:Destroy()
		end
	end
	
	GetItems()
	
	wait()
	SurfaceGui.Heading.Heading.Text = StatsName .. " Leaderboard"
	List.ListContent.GuideTopBar.Value.Text = StatsName
	List.CanvasSize = UDim2.new(0, 0, 0, ItemsFrame.UIListLayout.AbsoluteContentSize.Y + 35)
	wait(UpdateEvery)
	end
function AbbreviateNum(Number: number, Decimals:number?, Suffixes: {string}?)
	local Abbreviations = Suffixes or {"k", "M", "B", "T", "Qa", "Qn", "Sx", "Sp", "Oc", "N"}
	Number = tonumber(Number)
	Decimals = tonumber(Decimals)
	if not Number then return end
	return math.floor(((Number < 1 and Number) or math.floor(Number) / 10 ^ (math.log10(Number) - math.log10(Number) % 3)) * 10 ^ (Decimals or 3)) / 10 ^ (Decimals or 3)..(Abbreviations[math.floor(math.log10(Number) / 3)] or "")
end

How exactly do i implement this into the script to make it work?

1 Like

I’m not sure if I’m right or how but I feel like there is a better way to write that also if your way was to be used it’ll probably be better to place the table with all the letters outside of the function otherwise you’d just be creating a new function every time that function is called.

Also that math you did on the last line is confusing lol.

Probably something like

Money = AbbreviateNum(Number)

And i have a surfacegui and thats where the cash number is displayed so it does not really work

It won’t matter if that code he wrote is in gui or not you could just print it and it would do the same thing.

TextObject.Text = AbbreviateNum(Money)

Although the decimals are an issue if you wanted decimals, you have to go

TextObject.Text = AbbreviateNum(NumberWithoutDecimals, DecimalNumbers) which I don’t remember how to spilt them into their own numbers. So I suggest finding a new way. There will most likely be plenty of posts around on how to do this a better way.

your totally right! i just copied and pasted this code from my numbers utility module lol :joy:

Lol that code looked a bit horrible, I won’t lie.

You should definitely look at Miners Haven open source code; I was just looking at it… it’s called the MoneyLib and does exactly what you’re looking for be warned it is Copyrighted so therefore you’ll have to fork it and study it prior to rebooting your own releases

eh well it looks confusing if u don’t understand it

Its really that return line and me not knowing what log10 means and it just sounds bad to use for some reason lol.

it’s copyrighted and opensourced? never heard that before :joy:
anyways, i’ll check it out later

Just because its opensourced it doesn’t mean it’s not copyrighted. You can be opensourced but still cost money (My projects and Red hat). Open sourced means people can see the source and learn from it, copyrighted means that its illegal to steal it.

2 Likes

Here you go here is a good way.

local Suffixes = {"", "K", "M", "B", "T", "QA", "QI", "SX", "SP", "OC", "NO", "DC", "UD", "DD", "TD", "QAD", "QID", "SXD", "SPD", "OCD", "NOD", "VG", "UVG"}

function abbreviateNumber (n)
	local s = tostring(math.floor(n))
	return string.sub(s, 1, ((#s - 1) % 3) + 1) .. (Suffixes)[math.floor((#s - 1) / 3) + 1]
end

This was found via this Devforum post by @ABHI1290

Depending if there is a maximum amount of cash you’d like players to have then you can probably get rid of a few of the suffixes I don’t think players what have 1 Septillion cash (Assuming that’s what SP means)

I have another problem tho, how do i implement this in the game?
So like do i edit something in the main lb script or do i add another script?

Just go like

Text = abbreviateNumber(AmountOfMoneyPlayerHas)

Like this?

Yeah, I think so! Test it and see if it works.

image