Errors, but is working fine?

I am making a text label show the amount of cash they have, with the commas. I used

tostring(math.abs(amt)):reverse():gsub("%d%d%d", "%1,"):gsub(",$",""):reverse()

to show the amount of cash and it works perfectly fine, but, it errors at the “%1,” and says “invalid capture index”. Is there any reason for this or ways to fix it?

2 Likes
gsub("(%d%d%d)", "%1,")

Captures require enclosing parentheses.

Thank you, I never knew that they required parentheses as I don’t use gsub much.

Yeah, it’s quite a niche convention of “:gsub()” but it’s intuitive, suppose you wanted to do the following.

"(%d)(%d)(%d)", "%1,%2,%3"

No, I wanted to leave the numbers and have 1 comma at the end, which is already working with just

"(%d%d%d)", "%1,"

I know what you were trying to do, the above was just a demonstration of why string captures work in that way.

Oh, sorry, misinterpreted that :sweat_smile: but that seems quite confusing and honestly I’m trying to understand why/how they work like that, never thought string formats would be so confusing