Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Make a number into a shortcut(?)

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:

script.Parent.Text = game.Players.LocalPlayer.leaderstats.Cash.Value
while true do
	wait(0.1)
	script.Parent.Text = game.Players.LocalPlayer.leaderstats.Cash.Value
end

Here is the script I found but I tried using but couldn't get it to work:

local suffixes = {"K", "M", "B", "T", "Q", "Qu", "S", "Se", "O", "N", "D"}

local function toSuffixString(n)
    for i = #suffixes, 1, -1 do
        local v = math.pow(10, i * 3)
        if n >= v then
            return ("%.0f"):format(n / v) .. suffixes[i]
        end
    end
    return tostring(n)
end

script.Parent.Text = toSuffixString(game.Players.LocalPlayer.leaderstats.Cash.Value)

If you know how fix this, please let me know.

3 comments

0

The script you found is self explanatory, you just put the number in and it gives you the number with the suffix back. All you had to do was test it.

0

Also, make sure Cash is a string value

0

How do I do that?

2 answers

it is easy! Do it like:

local StarterGui = __________ -- Show startergui here
--Show like script.parent.parent.parent...(etc until it comes to the StarterGui)
--As you should use a normal script, using game.Players.LocalPlayer won't work

local cash = StarterGui.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")
		--keep continuing like that
	end
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.

1 comment

0

Sadly didn't work, and just set the value to 0.

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)

local function toSuffixString(n)
	local suffixes = {"K", "M", "B", "T", "Q", "Qu", "S", "Se", "O", "N", "D"}

	for i = #suffixes, 1, -1 do
		local v = math.pow(10, i * 3)
		if n >= v then
			return ("%.0f"):format(n / v) .. suffixes[i]
		end
	end
	return tostring(n)
end

--// 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....

spawn(function()
for i=0,35 do
	game.Players.LocalPlayer.leaderstats.Cash.Value = 10^i	
	wait(.1)
end
end)

script.Parent.Frame.Cash.Text = toSuffixString(game.Players.LocalPlayer.leaderstats.Cash.Value)
while true do
	wait(0.1)
	script.Parent.Frame.Cash.Text = toSuffixString(game.Players.LocalPlayer.leaderstats.Cash.Value)
end

And just incase the server script im using:

Code:

local Players = game:GetService("Players") 
Players.PlayerAdded:connect(function(plr)

	local LS = Instance.new("NumberValue",plr)
	LS.Name = "leaderstats"


	local Cash = Instance.new("NumberValue",LS)
	Cash.Name = "Cash"
	Cash.Value = "0"
end)

Hope this helps! :)

1 comment

0

Thank you for trying to help, sadly your script just loops it and puts it on the wrong abbreviation.

This discussion is preserved as it was. New answers, comments, and votes are closed.