Custom Coin Gui Bug

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 get no errors

1 Like

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.

4 Likes

ok so i get it,ima test this out and hopefully it works,as im still new to strings…etc

One thing do i just call the function normally or …

Simple, here is an example;

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.
1 Like

Wow it works but u have to reset ur character to get the new Value

Anyway you know how to make so when u get coins added to your leaderstats,your value increases to your new value?

Use a .Changed event for your leaderstat, and everytime it changes change the text.

1 Like

this is what i did

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)

but dosent seem to work idk rlly what to do

Fixed the issue thanks

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)
2 Likes