Instead of the function v1, which at times leaves only the most significant digit visible, you could use a function like this:
local abbrev = {"", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc"}
function abbrevNumber(n: number): string
local digits = math.floor(math.log10(n)) + 1
local abbrevIndex = math.ceil(digits / 3)
local numberAbbrev = abbrev[abbrevIndex]
local leftDigits, rightDigits, reducedNum
if digits > 3 then
leftDigits = (digits - 1) % 3 + 1
reducedNum = n / math.pow(10, digits - leftDigits)
rightDigits = 3 - leftDigits
else
leftDigits = digits
rightDigits = 0
reducedNum = n
end
local formatString = string.format("%%%d.%df%%s", leftDigits, rightDigits)
local newNumber = string.format(formatString, reducedNum, numberAbbrev)
return newNumber
end
for i = 0, 12 do
print(abbrevNumber(10 ^ i))
end
--// output:
--[[
1
10
100
1.00K
10.0K
100K
1.00M
10.0M
100M
1.00B
10.0B
100B
1.00T
]]
Then, instead of checking the leader stats every wait()
interval, you can make the code more efficient by only changing TextLabel.Text
when the Money
Value has been changed.
local abbrev = {"", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc"}
function abbrevNumber(n: number): string
local digits = math.floor(math.log10(n)) + 1
local abbrevIndex = math.ceil(digits / 3)
local numberAbbrev = abbrev[abbrevIndex]
local leftDigits, rightDigits, reducedNum
if digits > 3 then
leftDigits = (digits - 1) % 3 + 1
reducedNum = n / math.pow(10, digits - leftDigits)
rightDigits = 3 - leftDigits
else
leftDigits = digits
rightDigits = 0
reducedNum = n
end
local formatString = string.format("%%%d.%df%%s", leftDigits, rightDigits)
local newNumber = string.format(formatString, reducedNum, numberAbbrev)
return newNumber
end
local player = game:GetService("Players").LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local moneyValue = leaderstats:WaitForChild("Money")
local textLabel = script.Parent.TextLabel
moneyValue.Changed:Connect(function()
local value = money.Value
textLabel.Text = abbrevNumber(value)
end)