I’m trying to make a currency GUI that shows the players individual amount of coins, separate from the servers leader stats. Well I’m happy to say the Gui works its just when the players amount of coins gets very large, you can’t really see the amount of coins you have its all just zeros. I was wondering if there was a way to fix this, an example would be to change, 10000000 into 10M. Any help is appreciated, picture of the problem is below, along with the script.
Picture of the Problem
The Script
local stats = game.Players.LocalPlayer.leaderstats:FindFirstChild("Coins") -- Change "Money" To your sort of currency.
stats.Changed:Connect(function()
script.Parent.Text = "" ..stats.Value
end)
One way is to check the if the value is larger than… 1000 for example, if it is larger than you take the number divide by 1000 and just add a K behind it. But I think you should abbreviate the number.
local d = 10000000
local z = 1500000000
function shorten(y)
if (string.len(y)) <= 6 and (string.len(y)) >= 4 then
print(y/1000 .."k")
elseif (string.len(y)) <= 9 and (string.len(y)) >= 7 then
print(y/1000000 .."M")
elseif (string.len(y)) <= 11 and (string.len(y)) >= 8 then
print(y/1000000000 .."B")
end
print(string.len(y))
end
shorten(x)
shorten(d)
shorten(z)
Something like this would be able to get you going, I’ve provided the function which you can call below to see the output of for your testing. This will work for any number and with a little editing, you can have it display differently than I have it to work in your scenario.
currency_string=""
if money>math.pow(10,9) then
currency_string=math.floor(money/(math.pow(10,9)))..'B'
elseif money>math.pow(10,6) then
currency_string=math.floor(money/(math.pow(10,6)))..'M'
elseif money>math.pow(10,3) then
currency_string=math.floor(money/(math.pow(10,3)))..'K'
else
currency_string=money
end
Yes, currently in my script it is printing in the function so you would change that to return and use it as below
local stats = game.Players.LocalPlayer.leaderstats:FindFirstChild(“Coins”)
function shorten(y)
if (string.len(y)) <= 6 and (string.len(y)) >= 4 then
return(y/1000 .."k")
elseif (string.len(y)) <= 9 and (string.len(y)) >= 7 then
return(y/1000000 .."M")
elseif (string.len(y)) <= 11 and (string.len(y)) >= 8 then
return(y/1000000000 .."B")
end
end
stats.Changed:Connect(function()
local x = shorten(stats.Value)
script.Parent.Text = x
end)
Regarding adding more, you could continue going up the same way I did to get the numbers you want because we include a range, you can shorten numbers such as 1.5M and 15M both if you wanted as it does in the function above, similarly for 1.5B and 15B etc
Hey so I Ran into another problem, wondering if you can help me. The bigger numbers do add the M and B t the end, but when I add lets say, 20 to 2 billion I get 2 billion and 2, which creates another super long number. I was wondering if there was a way to just over right the small numbers so that it skips that small 20 and rounds either up or down, to the nearest highest number