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

How to insert a comma into a value?

I've got a script that displays the player's level as"'Bounty". If they are level 1 - 999 then it will display level 1 as 1,000, 2 as 2,000, 3 as 3,000 etc. But I'm not sure how to show 1mil bounty when they are level 1000. Here is the script that I'm using:

wait(3)
local Player = game.Players.LocalPlayer

while true do
	wait()
	if Player.Stats.Pirate.Value == 1 and Player.Stats.Level.Value <= 999 then
	script.Parent.Text = "Bounty:"..Player.Stats.Level.Value..",000"
	end
end

Thanks for reading!

2 comments

3

Wiki String Manipulation

0

lol

1 answer

✓ Accepted answer

An example from Desi...Let's take "250000" for example, if we use string.gsub("25000", "(%d%d%d)", "%1,") it will return 250,00 because it searches the string from left to right, so we have to reverse it.

string.reverse("25000"):gsub("(%d%d%d)", "%1,") will return "000,52" so we reverse it again to revert the process. string.reverse("25000"):gsub("(%d%d%d)", "%1,") :reverse() and that's it.

local function addComas(str)
    return #str % 3 == 0 and str:reverse():gsub("(%d%d%d)", "%1,"):reverse():sub(2) or str:reverse():gsub("(%d%d%d)", "%1,"):reverse()
end

print(addComas("250000000"))
print(addComas("25000000"))

3 comments

0

The string manipulation works, but when my level is 1001, it makes the bounty "1,001". Do you know how to make it display lvl 1000 as 1,000,000?

0

just multiply the number by 1000 before adding commas

0

thanks a bunch

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