Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i make my gui automatically turn a number that has more than 3 digits (1234) into (1,234)?

I have a Hud on roblox that shows your leader stats "Money,job, etc" But i want to script the money text so that if the amount of digits is greater than 3 there's a comma put for example " If the player just got 1000 i want the guis to display it as 1,000 not 1000. and so on for every number. 1,000 2,000 10,000 15,000 100,000,000 etc."

I know that i could just go into the script and say if the value == 1000 then gui.text = 1,000 but that would be way too much work as they players are going to be in the high digits. does anybody know how i can do this?

1 answer

✓ Accepted answer

This script I created will add the "," after every three numbers. You might want to turn this into a function.

Edit:- this uses a string so you may need to convert any numbers to string use this To string.

I can explain how modulo work in more detail if you need it just comment at the bottom.

Hope this helps.

local str = "1234" --input string
local output = "" --result
str =  string.reverse(str) --reverses the string so that the , go in the correct place

for i=1 , string.len(str) do --loop through all characters
	
	if i % 4 == 0 then --uses modulo which is the remainder from the division of i/4
		output = output .. "," .. string.sub(str, i, i)  --adds in the , incuding the number
	else
		output = output .. string.sub(str, i, i) -- no , needed
	end	
	
end

output =  string.reverse(output) --puts the string back to its correct format

print(output) -- prints the result

This discussion is preserved as it was. New answers, comments, and votes are closed.