I'm trying to make a script that changes a gui text to show "1m" instead of "1000000" as it's more neater and easier to read. I found a script that I could use but I'm not exactly 100% sure on how to use it on a gui text, here's the script in my gui:
1 | script.Parent.Text = game.Players.LocalPlayer.leaderstats.Cash.Value |
2 | while true do |
3 | wait( 0.1 ) |
4 | script.Parent.Text = game.Players.LocalPlayer.leaderstats.Cash.Value |
5 | end |
Here is the script I found but I tried using but couldn't get it to work:
01 | local suffixes = { "K" , "M" , "B" , "T" , "Q" , "Qu" , "S" , "Se" , "O" , "N" , "D" } |
02 |
03 | local function toSuffixString(n) |
04 | for i = #suffixes, 1 , - 1 do |
05 | local v = math.pow( 10 , i * 3 ) |
06 | if n > = v then |
07 | return ( "%.0f" ):format(n / v) .. suffixes [ i ] |
08 | end |
09 | end |
10 | return tostring (n) |
11 | end |
12 |
13 | script.Parent.Text = toSuffixString(game.Players.LocalPlayer.leaderstats.Cash.Value) |
If you know how fix this, please let me know.
it is easy! Do it like:
01 | local StarterGui = __________ -- Show startergui here |
02 | --Show like script.parent.parent.parent...(etc until it comes to the StarterGui) |
03 | --As you should use a normal script, using game.Players.LocalPlayer won't work |
04 |
05 | local cash = StarterGui.Parent.leaderstats.cash |
06 |
07 | cash.Changed:Connect( function () |
08 | if cash.Value > = 1000 and cash.Value < 1000000 then |
09 | local shortened = tostring (cash.Value/ 1000 ) |
10 | script.Parent.Text = (string.sub(cash, 1 , 2 ).. "K" ) |
11 | elseif cash.Value > = 1000000 and cash.Value < 1000000000 then |
12 | local shortened = tostring (cash.Value/ 1000000 ) |
13 | script.Parent.Text = (string.sub(cash, 1 , 2 ).. "B" ) |
14 | elseif cash.Value > = 1000000000 and cash.Value < 1000000000000 then |
15 | local shortened = tostring (cash.Value/ 1000000000 ) |
16 | script.Parent.Text = (string.sub(cash, 1 , 2 ).. "T" ) |
17 | --keep continuing like that |
18 | end |
19 | end |
And also, make sure you use a Normal script to increase the currency and put this script too as a normal script. Both should be normal script to work.
Just made a script that would do what you wanted, rather than changing your cash varable to a string vale i would leave that as a Number value and just hook up the function you found to the readout on your gui, Something like:
Gui Code: (Local Script)
01 | local function toSuffixString(n) |
02 | local suffixes = { "K" , "M" , "B" , "T" , "Q" , "Qu" , "S" , "Se" , "O" , "N" , "D" } |
03 |
04 | for i = #suffixes, 1 , - 1 do |
05 | local v = math.pow( 10 , i * 3 ) |
06 | if n > = v then |
07 | return ( "%.0f" ):format(n / v) .. suffixes [ i ] |
08 | end |
09 | end |
10 | return tostring (n) |
11 | end |
12 |
13 | --// this just changes your cash value so it shows the base 10 names in your suffixes table by powering it by 10 aka `10^0 = 1` , `10^1 = 10`, `10^2 = 100` etc.... |
14 |
15 | spawn( function () |
And just incase the server script im using:
Code:
01 | local Players = game:GetService( "Players" ) |
02 | Players.PlayerAdded:connect( function (plr) |
03 |
04 | local LS = Instance.new( "NumberValue" ,plr) |
05 | LS.Name = "leaderstats" |
06 |
07 |
08 | local Cash = Instance.new( "NumberValue" ,LS) |
09 | Cash.Name = "Cash" |
10 | Cash.Value = "0" |
11 | end ) |
Hope this helps! :)