Please Number Abbreviation

So I wrote a Module script to abbreviate numbers in my game server, through Replicated Storage. Then I used the Module Script in a Local Script through a starter Gui, that will appear to the player showing them the amount of currency they have through that GUI. You might have saw my post earlier about how my the data wasn’t displaying on the GUI. Well I got the data to display on a GUI, but the number was extremely long, so I had to make a number abbreviation script to make the numbers shorter. Like turning, 10 millions into 10M. Well the script isn’t working and I’d love some help on how to fix it.

Module Script Titled “AbbreviateNumber” in Replicated Storage

local stats = game.Players.LocalPlayer.leaderstats:FindFirstChild("Coins")

local AbbreviateNumber = {}

local abbreviations = {
	K = 4,
	M = 7,
	B = 10,
	T = 13,
	Qa = 16,
}

function AbbreviateNumber:Abbreviate(stats)
	local text = tostring(math.floor(stats))

	local chosenAbbreviation
	for abbreviation, digits in pairs(abbreviations) do
		if #text >= digits and #text < (digits + 3) then
			chosenAbbreviation = abbreviation
			break
		end
	end

	if chosenAbbreviation then 
		local digits = abbreviations[chosenAbbreviation]
	
		local rounded = math.floor(stats / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
		
		text = "$" .. string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbreviation
	else
		text = "$" .. stats
	end 
	
	return text
end

return AbbreviateNumber

The Local Script in The Text Label Titled “Abbreviating Number”

local AbbreviateNumber = require(game.ReplicatedStorage.AbbreviateNumber)

local stats = game.Players.LocalPlayer.leaderstats:FindFirstChild("Coins") -- Change "Coins" To your sort of currency.

local text = AbbreviateNumber:Abbreviate(stats)

print(text)

script.Parent.Text = text

stats.Changed:Connect(function()

local x = stats.Value

script.Parent.Text = x

end)
2 Likes

It looks like the stats argument in the AbbreviateNumber:Abbreviation() function gets the NumberValue Instance instead of it’s value. You should just add .Value to the right of stats when you’re first assigning the text variable in the local script.

You could also do the same thing in the :Abbreviate() function to not repeat yourself, but you’d be unable to abbreviate numbers if they weren’t in a ValueObject.

Use number as an argument its because we already passed it within the client the number is referenced YourNameValue.Value so we dont need to get it from the server

And When You Use Abbreviate()

You Should do this

Module:Abbreviate(Number.Value)
1 Like