New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to insert a comma into a value?

Asked by
soutpansa 118
1 year ago
Edited 1 year 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 3935 — 1y
0
lol User#19524 106 — 1y

1 answer

Log in to vote
2
Answered by
ABK2017 392 Moderation Voter
1 year ago
Edited 1 year 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 118 — 1y
0
just multiply the number by 1000 before adding commas User#22604 1 — 1y
0
thanks a bunch soutpansa 118 — 1y
Ad

Answer this question