Cash GUI with Commas

You can write your topic however you want, but you need to answer these questions:

  1. 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?
  2. What is the issue? Include screenshots / videos if possible!
    I’m not sure on how to make it have automatic commas.
  3. 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
1 Like

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
1 Like

Use :GetPropertyChangedSignal, a while loop will take up a LOAD of performance.

2 Likes

How would I do that? ADDING CHARACTERS

CashLabel = script.Parent.Parent.CashLabel
Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats").Cash

Leaderstats.Changed:Connect(function()
    CashLabel.Text = "$ "..format_int(Leaderstats.Value)
end)

This should work. You can use the format_int function that @Volieb provided. You should usually prefer events over while loops whenever possible.

Wait, ok… Question is there any easier way to add automatic commas?

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.

The one Vinny gave, I don’t fully understand any of the code. How could I code my own automatic comma system

Also I did this code, and it ain’t working. Hmm

CashLabel = script.Parent.Parent.CashLabel
Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats").Cash.Value

Leaderstats.Changed:Connect(function()
	CashLabel.Text = "$ "..Leaderstats
end)

That’s not the code I provided. Please look at the code I sent and use that.

I wasn’t sure what the format inc was so I left it out

Also what bout the “The one Vinny gave, I don’t fully understand any of the code. How could I code my own automatic comma system” question

I’ll give a shot at explaining @Volieb’s code.

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
2 Likes

My mind reading this:

:brain::boom:

1 Like