You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
How can I make the cash GUI have automatic commas includes when it reaches like the thousands place and so on?
What is the issue? Include screenshots / videos if possible!
I’m not sure on how to make it have automatic commas.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I couldn’t really find anything anywhere.
CashLabel = script.Parent.Parent.CashLabel
Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats").Cash.Value
while true do
wait()
CashLabel.Text = "$ "..Leaderstats
end
Using the function below, you may pass the leaderstat value, and it will henceforth return a string version with the appropiate commas. Please be aware that the below code is not mine and sourced from stackoverflow.
function format_int(number)
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
int = int:reverse():gsub("(%d%d%d)", "%1,")
return minus .. int:reverse():gsub("^,", "") .. fraction
end
If you’re asking if there’s some property or function already provided by Lua or Roblox, then no. You’d have to code an automatic comma placement function yourself. Thankfully, you can just use the function in this post.
It’s a good answer to your question, complicated or not.
You could instead use roblox’s Localization tables to auto-translate numbers to the local language including comma vs. decimal separators, but that’s gonna be more complex.
function format_int(number)
-- 1. pull apart the number
local i, j, minus, int, fraction = -- string.find returns a bunch of things
tostring(number) -- convert your number to a string
:find( -- find the following snippets inside it:
'([-]?)' .. -- `minus`: 0 or 1 "-" character
'(%d+)' .. -- `int`: 1 or more numbers in a row
'([.]?%d*)' -- `fraction`: 0 or 1 "." character, followed by 0 or more numbers in a row
)
-- 2. add commas to the non-fraction part of the number
int = -- modify the `int` part (between the (optional) minus sign and the (optional) decimal part)
int:reverse() -- flip it backwards so we can easily add a comma every 3 numbers
:gsub( -- replace...
"(%d%d%d)", -- ...every group of three digits in a row...
"%1," -- ...with the same three digits followed by a comma (everything is still reversed)
)
-- 3. put the parts back together
return minus .. -- if our number had a "-" in front, add it here
int:reverse() -- flip the previously-reversed integer part back around
:gsub("^,", "") .. -- a comma might have ended up as the first character, remove that if it's there
fraction -- stick on the decimal and everything following
end