Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to insert a comma into a value?

Asked by
soutpansa 120
6 years ago
Edited 6 years ago

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!

3
Wiki String Manipulation Shawnyg 4330 — 6y
0
lol User#19524 175 — 6y

1 answer

Log in to vote
2
Answered by
ABK2017 406 Moderation Voter
6 years ago
Edited 6 years ago

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"))
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? soutpansa 120 — 6y
0
just multiply the number by 1000 before adding commas User#22604 1 — 6y
0
thanks a bunch soutpansa 120 — 6y
Ad

Answer this question