Hello Developers!
A bit of a backstory before I go into my issue (so everything makes sense).
I was trying to see what happened in my GUI when I collected a coin that would have given like 2t coins. It didn’t let me collect it because that amount of numbers could not fit in that single GUI. So I changed the code so if the player gets 1M+ coins, it would say “1.00M+”.
My issue is, I cannot figure out how to make it so it says the exact decimal. For example, if someone had 1,234,567 coins, I want the GUI to say “1.23M+”. (Same goes for billion, trillion, and on.")
Here is the code i am using so far:
local player = game.Players.LocalPlayer
while wait() do
script.Parent.Text = player.leaderstats.Coins.Value
if player.leaderstats.Coins.Value >= 1000000 then
script.Parent.Text = "1.00M+"
end
end
If anyone could help me with this it would be great! Thanks!
Im still learning so sorry if this is something very simple to do.
You can use string formatting to specify the number of decimals. If you have a static list of orders of magnitude you want to use (ex. if you only ever want to use million, billion, and trillion and not cover cases that go any higher) you can use conditionals:
function formatCoins(amount)
local order = nil
if amount < math.pow(10,6) then --1 million
return tostring(amount)
elseif amount < math.pow(10, 9) then --1 billion
return string.format("%.2fM+", amount/math.pow(10,6))
elseif amount < math.pow(10,12) then --1 trillion
return string.format("%.2fB+", amount/math.pow(10,9))
else
return string.format("%.2fT+", amount/math.pow(10,12))
end
end
local abbreviations = {"", "K", "M", "B", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc", "Un", "Duo", "Tre", "Qua", "Qui", "Sed", "Sep", "Oct", "Nov", "V"}
-- You can remove or add if you feel
-- 100Qa, 700Oc, 999Sep, etc.
local function Format(value, idp)
local ex = math.floor(math.log(math.max(1, math.abs(value)),1000))
local abbrevs = abbreviations [1 + ex] or ("e+"..ex)
local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)
return ("%."..idp.."f%s"):format(normal, abbrevs)
end
while wait() do
local player = game.Players.LocalPlayer
script.Parent.Text = Format(player.leaderstats.Coins.Value, 2)
-- Change the "Defense" to the name of your leaderstat Name not variable
end
Thank you both, that really helps, I wish I could give 2 solutions but I can’t so I’ll give it to Astro because his script made it work, lol. But again thank you both for the lesson, and I’ll definitely look into that link on formatting and converting strings!
sorry didnt mean to solution myself
ok, sorry for the bother, but when i first tried the script everything worked, it worked for thousands too (thanks lol) but now it isnt working for those numbers at all, only 1m and up. Is there anything I can add to make it work?
BTW the 2 that he passed in the second parameter of the function is what tells it to have 2 numbers after the decimal, if you want more or less than just tweak the number.
No problem, I just wanted to let you know so you know how to customize it (also I always use 2). I always use this script, idk if that guy got it from the devforum post I used or a YouTube video (such as mine on this topic)