Separate Ui text that show a value

Hello devs,

I’m stuck with a small thing and i’m wondering if someone can help me about that.

So i have a gui text that show the value of a “IntValue”, and i want to separate the gui text with a “,” for each 3 number the value have.
It’s hard to explain, so there is a picture exemple bellow.

Edit: i don’t want letter abreviation, because the value cap is 999.999.999

Capture

hmm i would approach this problem by getting the length of the int value as a string, then for every 3 characters a comma would be added to the string.

local numStr = tostring(coins) -- the coin number as a string
local numChars = numStr:len()
local newStr = "" -- the new string you're constructing
for i = 1, numChars do
	local charIndex = numChars - (i - 1)
	if i ~= 1 and (i-1)%3 == 0 then
		-- comma should not be added at the beginning of a string
		-- the comma is added since it is a third digit
		newStr = numStr:sub(charIndex, charIndex) .. "," .. newStr
	else
		-- comma is not added since it is not a third digit
		newStr = numStr:sub(charIndex, charIndex) .. newStr
	end
end

haven’t tested this, so i would just use it as pseudocode for now.

edit: just remembered, you would have to go starting from the back of the string, not from the begninning of the string. i editted it so you can use it starting from the back of the string

1 Like

Alright it work, thank you so much for the help !

local Player = game.Players.LocalPlayer
local Money = Player:WaitForChild("Money" ,5)

if Money ~= nil then
	local CoinsFrame = script.Parent:WaitForChild("Coins" ,5)	
	local Coins = Money:WaitForChild("Coins" ,5)
	
	if CoinsFrame ~= nil and Coins ~= nil then
		
		local function Update()
			local numStr = tostring(Coins.Value)
			local numChars = numStr:len()
			local newStr = ""

			for i = 1, numChars do
				local charIndex = numChars - (i - 1)
				
				if i ~= 1 and (i-1)%3 == 0 then
					newStr = numStr:sub(charIndex, charIndex) .. "," .. newStr
					CoinsFrame.Value.Text = newStr
				else
					newStr = numStr:sub(charIndex, charIndex) .. newStr
					CoinsFrame.Value.Text = newStr
				end
			end
		end
		
		Update()
		Coins.Changed:Connect(function()
			Update()
		end)
	end
end
1 Like

no problem! glad i could help out! :smirk_cat:

1 Like