What do I want to achieve?
I want the IntValue to be in 1k, 2k, or 1b (billion), so on
What is the issue?
Well, roblox doesnt support value to be written in 1k, 2k, 3k.
What solutions have I tried so far?
I have tried to do it like this -
local Cash = script.Parent
local TextLabel = script.Parent.Parent.Part.Display.SurfaceGui.TextLabel
Cash.Changed:Connect(function()
TextLabel.Text = "$"..Cash.Value
if Cash.Value == 1000 then
TextLabel.Text = "$1k"
end
if Cash.Value == 1100 then
TextLabel.Text = "$1.1k"
end
if Cash.Value == 1200 then
TextLabel.Text = "$1.2k"
end
if Cash.Value == 1300 then
TextLabel.Text = "$1.3k"
end
end)
To summarize I basically wanted the TextLabel.Text Value to display numbers above 999, like 1k, 2k, 3k, 100k, as such. I could’nt think of any other way, so need help.
i’d go around this by not changing the intvalue at all and making an algorithm that changes the text according to the intvalue. i’d do this by checking if the cash is above 10^x which is when i’d start using the prefix associated with it (if above 10^3 aka 1000, start using k and if above 10^6 or millions start using m) and dividing the intvalue by their respective power (e.g if intvalue is 1200, divide by 10^3). if this sounds too complicated thats because it somewhat is due to the fact this way is purely mathematically based.
example:
local cashString = tostring(cashValue) --cashValue is a placeholder for how much cash we have
local prefix = -- insert prefix here
local power = 0 --current exponent
if cashValue > 10^3 then
power = 10^3
prefix = "k"
cashString = tostring(cashValue / power) .. prefix
elseif cashValue > 10^6 then
power = 10^6
prefix = "m"
cashString = tostring(cashValue / power) .. prefix
-- and so on.
QUICK SIDENOTE: i just realized that each new prefix comes with every three, so you can make a table and check if the number is larger than 10^(x+3). a good example is that 10^3 is thousands. 10^6 is millions, 10^9 is the billions and 10^12 is a trillion
local value = script.Parent.Value -- your value HERE
local textLabel = script.Parent -- your label
local function formatNumber(number)
if number >= 1000000000000 then
local formatted = tostring(math.floor(number / 1000000000000)) .. "t"
return formatted
elseif number >= 1000000000 then
local formatted = tostring(math.floor(number / 1000000000)) .. "b"
return formatted
elseif number >= 1000000 then
local formatted = tostring(math.floor(number / 1000000)) .. "m"
return formatted
elseif number >= 1000 then
local formatted = tostring(math.floor(number / 1000)) .. "k"
return formatted
else
return number
end
end
value.Changed:Connect(function(newVal)
textLabel.Text = formatNumber(newVal)
end)
local nots = {"K","M","B","T","Qd","Qn","Sx","Sp","O","N"} -- i stole this
function shorten(num)
local notation = ""
for _, _not in next, nots do
if (num / math.pow(10, _ * 3)) >= 1 then notation = _not end
end
local front = (#tostring(num) % 3 == 0) and tostring(num):sub(0, 3) or tostring(num):sub(0, #tostring(num) % 3)
return front .. notation
end
print(shorten(6942069420)) -- 6b
shorter but you can keep the guy earlier as the solution*
Yes it is, all you have to do is change local value = script.Parent.Value to local value = player.leaderstats.Cash.Value make sure to make a player variable too
Bro i tried doing the same thing with leaderstats, like i thought making a local script which fires an event when the cash.value changed then made a seperate server script which on that event fired makes the value of the leaderstats.cash.value to 1k, 2k. But it didnt work.
Pls Help bro, how do I implement ur script in my game? Should I put ur script in my leaderstats script or make a seperate one? @IRoastToaster
the value being 1k 1.2k etc, should only be client side, u dont have to worry and make a remote event and fire, the player that is playing the game should see his cash and not the whole server! just put my script in a LocalScript in the TextLabel GUI you made and change the first line of my script to
local player = game.Players.LocalPlayer
local value = player.leaderstats.Cash