I’ve been trying to create a coin gui system with commas…etc but no one my ways never seem to work
local Player = game.Players.LocalPlayer
local Number = Player.leaderstats.Coins.Value
local function Round(Int)
local NewNumber
local Length = string.len(Int)
if Length == 4 then
NewNumber = string.sub(Int, 1, 1) .. "." .. string.sub(Int, 2, 3) .. "K+"
end
if Length == 5 then
NewNumber = string.sub(Int, 1, 2) .. "." .. string.sub(Int, 3, 4) .. "K+"
end
if Length == 6 then
NewNumber = string.sub(Int, 1, 3) .. "." .. string.sub(Int, 4, 5) .. "K+"
end
if Length == 7 then
NewNumber = string.sub(Int, 1, 1) .. "." .. string.sub(Int, 2, 3) .. "M+"
end
if Length == 8 then
NewNumber = string.sub(Int, 1, 2) .. "." .. string.sub(Int, 3, 4) .. "M+"
end
if Length == 9 then
NewNumber = string.sub(Int, 1, 3) .. "." .. string.sub(Int, 4, 5) .. "M+"
end
return NewNumber
end
script.Parent.Text = Number
I don’t recommend using your method. What you should do is return a function for the text;
function addComma(number)
local left, num, right = string.match(number, '^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
All you have to do is call the function, and the function returns the text with commas. I use this for any game I have that I need currency displayed. Hope this helped!
Also please note, this doesn’t add M+ , K+ , this adds a comma like it does in the Leaderboard. If you wish to know how to add M and K’s to your text here is a video that can explain it well for you;
What you do with that is just change what he does for the leaderboard and do it in the text.
local numbermine = 1000 -- this is a number, this is what a leaderstat would look like
function addComma(number)
local left, num, right = string.match(number, '^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
script.Parent.Text = addComma(tostring(numbermine)) --that is a good example of how you should do it, converting the number to a string is the only way the function will return the number properly for a text.
local Players = game.Players.LocalPlayer
local numbermine = Players.leaderstats.Coins.Value -- this is a number, this is what a leaderstat would look like
function addComma(number)
local left, num, right = string.match(number, '^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
game.Players.LocalPlayer.leaderstats.Coins.Changed:Connect(function()
script.Parent.Text = addComma(tostring(numbermine))
end)
local Players = game.Players.LocalPlayer
local numbermine = Players.leaderstats.Coins -- this is a number, this is what a leaderstat would look like
function addComma(number)
local left, num, right = string.match(number, '^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
game.Players.LocalPlayer.leaderstats.Coins.Changed:Connect(function()
script.Parent.Text = addComma(tostring(numbermine.Value))
end)