Formatting issue

So i was working on changing my money to showcase on the UI, and I decided to format it with commas, with a function that works.
Here is the function I used:

function MathModule.formatWithCommas(number)
	local formatted = number
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if k == 0 then
			break
		end
	end
	return formatted
end

In my main script this is the problem, and I’ve gone through several print statements to figure it out. Here is what I obtained in the output:
5,000
5,000 string
false
5,000​ 5,000
false
5000 5,000
from this code:

local function updateCredits()
	local formatted = MathUtil.formatWithCommas(creditsValue.Value)
	print(formatted)
	print(tostring(formatted), type(formatted))
	
	creditsLabel.Text = formatted
	print(creditsLabel.AutoLocalize)
	print(creditsLabel.Text, formatted)
	task.wait(0.1)
	print(creditsLabel.AutoLocalize)
	print(creditsLabel.Text,formatted)
end

this made no sense to me as I figured it was autolocalize, or the type being a number, but all of it should be just fine. It changes the text directly to 5000 instead of 5,000 automatically even when I set it differently. I do not set it anywhere else in my code, this is the only script here that even references the creditsLabel. It’s happening to my other label as well though. I’m not sure if this is a studio bug, or something else that I just don’t know.

1 Like

I’m confused, what exactly is the issue, is it the fact that the creditsLabel.Text shows as 5000 or is it because of something else, just wanted to clarify before helping!

Sorry for the long reply, but yes the issue is that the actual text label shows as 5000. Yet when I get it formatted it gives me 5,000. It’s mean to format it as a comment, but it doesn’t. Infact, after 0.1 seconds, it changes the text without any other script effecting it. Meaning it changed the text from 5,000 (what it was set to earlier) to 5000 automatically when I didn’t want that.

it works in my game:

function formatWithCommas(number)
	local formatted = number
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if k == 0 then
			break
		end
	end
	return formatted
end

local function updateCredits()
	local formatted = formatWithCommas(5000)
	print(formatted)
	print(tostring(formatted), type(formatted))

	local c = Instance.new("TextLabel", game.StarterGui.MainMenu)
	c.Text = formatted
	c.AutoLocalize = false
	print(c.AutoLocalize)
	print(c.Text)
	task.wait(0.1)
	print(c.AutoLocalize)
	print(formatted)
end

updateCredits()



(i have set the size manually from properties)
also i tried both autolocalized on and off and it still works