Hello , I’m scripter developer as you can guess and I have little issue that I can’t solve . Maybe you can help me with it , I will really appreciate it !
So I want to make numbering system , like when player have > 1000 strength it will add “K” on the ending and if he have > 1000000 then it will make “M” on the ending I tried making script and here what it looks like .
Script
local dady = script.Parent
local Player = game.Players.LocalPlayer
while true do
dady.Text = Player.leaderstats.coins.Value
wait(0.3)
elseif Player.leaderstats.coins.Value >= 1000 Player.leaderstats.coins.Value <= 1000000 then
You should only update the text when the value changes, for that you can use the changed event of the value instance.
Edit: The replies below are better.
local dady = script.Parent
local endings = {
"k",
"M",
"B"
}
local function updateText(num)
local numAsString = tostring(num)
local numsBeforeDecimals = numAsString:find(".")-1 or numAsString:len()
-- 1000 has 4, 1000000 has 7 and 1000000000 has 10 numbers before the decimals.
-- By reducing 1 from that, we get a number that can be used
-- to calculate which letter will be in the end (couldn't figure out a proper mame for the variable)
local minus1 = numsBeforeDecimals-1
local exp = minus1-minus1%3
local ending = endings[exp/3]
local text = num/10^exp .. ending
dady.Text = text
end
I was actually working on something like this a little while ago, I went to the developer forums for help and got this script: (I created an explanation for a few things as well, I encourage you to read my explanations to understand the script better)
local log10 = math.log10
--[[
math.log10 finds the amount of 0s in a number
for example: math.log10(10) = 1
If you did a number like 12, you'd get a very long decimal at the end
that's where math.floor comes in
]]
local floor = math.floor
--[[
math.floor rounds numbers down
for example: math.floor(2.898723945) = 2
which also means
math.floor(math.log10(13)) = 1
]]
local min = math.min
--[[
math.min finds the lowest value in a set of numbers
for example: math.min(1,3) = 1
which also means
math.min(3,1) = 1
]]
local abbreviations = {"K","M","B","T"}
--[[
The table above is a list of abbreviations (K,M,B,T)
Makes sure you're making this table in order
You wouldn't do: {"K","B","M"}
because millions come before billions
]]
function format(n) --The parameter "n" in this case is the number we're abbreviating
local b = min(floor(log10(n) / 3), #abbreviations)
--[[
The line above finds the create abbreviation for "n" if there is one on the "abbreviations" table
I did log10(n)/3 because each of the indexes on the abbreviations table are 3 numbers apart
What do I mean by this?
The difference between the length of floor(log10(1000)) and floor(log10(1000000)) is 3
]]
local abbr = abbreviations[b]
--[[
The line above finds the abbreviation needed (or nil) in the abbreviations table
]]
if abbr then
else
--[[
If the number doesn't need to be abbreviated then we return the number as is
]]
return tostring(n)
end
--[[
However if the number does have to be abbreviated the following will happen
]]
local s = ("%.2f%s"):format(n / (1000^b), abbr):gsub("%.00","")
--[[
The line abbreviates the number in such a way that
1000 = 1K
1250 = 1.25K
1200 = 1.2K
]]
local x = s:find("%..0")
if x then
s = s:sub(1,x+1)
end
return s
--[[
Returns the abbreviated number
]]
end
If you want to convert your text you could do something like this: (just an example)
local label = script.Parent
print(format(label.Text))
I’d like to add that this wouldn’t be possible without the help I received from @bytechan and @Ninja_Deer
The post I made asking for help on the same issue is below:
while wait() do
local player = game.Players.LocalPlayer
local number = player.leaderstats.Coins.Value -- change this to your currency
local abbriviation = {
K = 4, -- Thousand
M = 7, -- Million
B = 10, -- Billion
T = 13, -- Trillion
Qa = 16, -- Quadrillion
Qi = 19, -- Quintillion
SI = 22, -- Sextillion
SP = 25, -- Septillion
Oc = 28, -- Octillion
N = 31, -- Nonillion
DC = 34 -- Decillion+
}
local text = tostring(math.floor(number))
local chosenAbbriviation
for abbriviation, digits in pairs(abbriviation) do
if #text >= digits and #text < (digits + 3) then
chosenAbbriviation = abbriviation
break
end
end
if chosenAbbriviation then
local digits = abbriviation[chosenAbbriviation]
local rounded = math.floor(number / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
text = string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbriviation
else
text = number
end
script.Parent.Text = text
end