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):
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)
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)