Comma Module adding an extra comma at the beginning

Hello! I made this quick and simple module which will add commas to numbers that I put in, but it’s printing this
image
If I do like 500,000 etcetera.

This is the module:

return function(p1)
    p1 = tostring(p1)
	return #p1 % 3 == 0 and p1:reverse():gsub("(%d%d%d)", "%1,"):reverse():sub(2) or p1:reverse():gsub("(%d%d%d)", "%1,"):reverse()
end;

If you have any ideas or know how to fix it, it’ll be greatly appreciated! Thanks :smile:

If anyone knows, please let me know! It’ll be very much appreciated :smile:

Not sure if this helps, but this is an example of how I add commas in my game.

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

print(comma_value(12000)) -- 12,000
2 Likes