Shortening A Number

Hi, I’m trying to make a script that shortens the number so it changes to “1m” instead of “1000000”. I’ve got a script working but for some reason it just sets the text to “0”. Here is the script:

local player = game.Players.LocalPlayer


local cash = player.Parent.leaderstats.cash

cash.Changed:Connect(function()
	if cash.Value >= 1000 and cash.Value < 1000000 then
		local shortened = tostring(cash.Value/1000)
		script.Parent.Text = (string.sub(cash, 1, 2).."K")
	elseif cash.Value >= 1000000 and cash.Value < 1000000000 then
		local shortened = tostring(cash.Value/1000000)
		script.Parent.Text = (string.sub(cash, 1, 2).."B")
	elseif cash.Value >= 1000000000 and cash.Value < 1000000000000 then
		local shortened = tostring(cash.Value/1000000000)
		script.Parent.Text = (string.sub(cash, 1, 2).."T")

	end
end

Here is the gui in the explorer (script is now a local script):
Screen Shot 2020-09-18 at 8.02.37 AM

If you can help me out, please do. Thanks.

1 Like

Why do you need a serverscript to do this again?

Localscripts can see everything that’s not in ServerScriptService or ServerStorage.

3 Likes

I’m not 100% sure why I’m using a serverscript, but would this worked if i defined it this way?

local StarterGui = game.StarterGui

No, you would need to use a local script. I suggest looking at this


I get this error in the output when I used a local script, here is the script I used:

local Player = game.Players.LocalPlayer

local PlayerGui = Player:WaitForChild("PlayerGui")


local cash = Player.leaderstats.Cash

cash.Changed:Connect(function()
	if cash.Value >= 1000 and cash.Value < 1000000 then
		local shortened = tostring(cash.Value/1000)
		script.Parent.Text = (string.sub(cash, 1, 2).."K")
	elseif cash.Value >= 1000000 and cash.Value < 1000000000 then
		local shortened = tostring(cash.Value/1000000)
		script.Parent.Text = (string.sub(cash, 1, 2).."B")
	elseif cash.Value >= 1000000000 and cash.Value < 1000000000000 then
		local shortened = tostring(cash.Value/1000000000)
		script.Parent.Text = (string.sub(cash, 1, 2).."T")

	end
end)

Any idea why?

Yeah, because every few lines you’re trying to sub using ‘cash’ instead of ‘cash.Value’

That script will not be best to use as it won’t get the job done. What you should do is the following:

local abbreviations = {
    "K", -- 4 digits
    "M", -- 7 digits
    "B", -- 10 digits
    "T", -- 13 digits
    "QD", -- 16 digits
    "QT", -- 19 digits
    "SXT", -- 22 digits
    "SEPT", -- 25 digits
    "OCT", -- 28 digits
    "NON", -- 31 digits
    "DEC", -- 34 digits
    "UDEC", -- 37 digits
    "DDEC", -- 40 digits
}

local function Abbreviate(x)
    if x < 1000 then 
        return tostring(x)
    end

    local digits = math.floor(math.log10(x)) + 1
    local index = math.min(#abbreviations, math.floor((digits - 1) / 3))
    local front = x / math.pow(10, index * 3)

    return string.format("%i%s+", front, abbreviations[index])
end

script.Parent.Text = Abbreviate(cash.Value)

EDIT: KEEP your variables

Screen Shot 2020-09-18 at 8.52.38 AM
I tried using that but it just sets the it to 0 for some reason.

Any errors? Do you use a datastore?

Nothing in the output, I do use datastores.

Can I see your datastore script. This will help set the value…

Sure, it’s a little long, here you go:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("gold")
local ds2 = datastore:GetDataStore("cash")
local ds3 = datastore:GetDataStore("rebirth")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Parent = leaderstats
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirths"
	rebirth.Parent = leaderstats

	local playerUserId = "Player_"..player.UserId

	gold.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, gold.Value)
	
	cash.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, cash.Value)
	
	rebirth.Value = ds3:GetAsync(player.UserId) or 0
	ds3:SetAsync(player.UserId, rebirth.Value)
	
	gold.Changed:connect(function()
		ds1:SetAsync(player.UserId, gold.Value)
	end)
	
	cash.Changed:connect(function()
		ds2:SetAsync(player.UserId, cash.Value)
	end)
	
	rebirth.Changed:connect(function()
		ds3:SetAsync(player.UserId, rebirth.Value)
	end)
	
	
end)

Do the following:
Create a remote event named RemoteEvent

In Datastore script:

cash.Changed:connect(function()
		ds2:SetAsync(player.UserId, cash.Value)
game.ReplicatedStorage.RemoteEvent:FireClient(player, cash.Value)
	end)
	
end)

In Local Script (ADD THIS BEFORE THE ABBREVIATION STUFF):

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(Function(cash))
script.Parent.Text = cash
end)

So like this?

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(cash)
script.Parent.Text = cash
end)

local Player = game.Players.LocalPlayer

local PlayerGui = Player:WaitForChild("PlayerGui")


local cash = Player.leaderstats.Cash


local abbreviations = {
	"K", -- 4 digits
	"M", -- 7 digits
	"B", -- 10 digits
	"T", -- 13 digits
	"QD", -- 16 digits
	"QT", -- 19 digits
	"SXT", -- 22 digits
	"SEPT", -- 25 digits
	"OCT", -- 28 digits
	"NON", -- 31 digits
	"DEC", -- 34 digits
	"UDEC", -- 37 digits
	"DDEC", -- 40 digits
}

local function Abbreviate(x)
	if x < 1000 then 
		return tostring(x)
	end

	local digits = math.floor(math.log10(x)) + 1
	local index = math.min(#abbreviations, math.floor((digits - 1) / 3))
	local front = x / math.pow(10, index * 3)

	return string.format("%i%s+", front, abbreviations[index])
end

script.Parent.Text = Abbreviate(cash.Value)

Yes, any errors? Anything not working?

Sadly just sets the value back to 0, should I remove this?

local cash = Player.leaderstats.Cash

this was worked on in PMs. Here are the 2 scripts:

Server

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MarketplaceService = game:GetService("MarketplaceService")

local defaultCashValue = 0

game.Players.PlayerAdded:Connect(function(player)
	local coinsDataStore = DataStore2("Coins", player)
	
	local cash = Instance.new("StringValue")
	cash.Name = "Cash"
	
	local function coinsUpdated(updatedValue)
		cash.Value = coinsDataStore:Get(updatedValue)
		game.ReplicatedStorage.UpdateClientCurrency:FireClient(player, coinsDataStore:Get(defaultCashValue))
	end


	coinsUpdated(defaultCashValue)
	
	coinsDataStore:OnUpdate(coinsUpdated)
	


end)

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.UpdateClientCurrency.OnClientEvent:Connect(function(updatedValue)
	local Suffixes = {'k', 'M', 'B', 'T'}
	local function shorten(value)
		if updatedValue < 10000 then
			return value
		end
		local i = math.min(math.floor(math.log(value, 10000)), #Suffixes)
		return math.floor(value / (10000 ^ i)) .. Suffixes[i] .. '+'
	end
	script.Parent.Text = shorten(updatedValue)
end)
2 Likes