How do I shorten leaderstat numbers?

Hello there fellow robloxian.

I have some leaderstats called tix. And I’m trying to make it shorter so it shows…

1k+

Instead of it saying…

1000

So where my leaderstats script’s location is.
image

And the script for it is.

game.Players.ChildAdded:Connect(function(p)
	local leaderstats = Instance.new("IntValue",p)
	leaderstats.Name = "leaderstats"
	
	local tix = Instance.new("StringValue", leaderstats)
	tix.Name = "Tix"
end)

Any help is appreciated. Thank you and have a good day. I can’t explain very well due to language problems. Sorry :frowning:

P.S: If your answer is correct and is functional, your message will be selected as a solution.

I’d use logarithms by 1000 then divide the value, something like this.

local suffxes = {'k', 'M', 'B', 'T', ...}
local function shorten(value)
   if value < 1000 then
      return value
   end
   local i = math.min(math.floor(math.log(value, 1000)), #suffixes)
   return math.floor(value / (1000 ^ i)) .. suffixes[i] .. '+'
end
3 Likes

Where do i position the code? in a script / local script or module script? or in server script storage or whatever.

Wherever you prefer. I’d personally store it as a ModuleScript in the ReplicatedStorage, but it’s your choice.

I’ll try, thank you for the shortened leaderstats script.

It does not work.

Unfortunately.

It works well, just the table was mispelled as suffxes instead of suffixes at the beginning - simply reading over the code solved this.

You could use open-source modules for this too, e.g:

2 Likes