Using Bnum/Eternitynum with leaderstats

How would I use Bnum/Eternitynum modulescripts with leaderstats (profileservice)? Currently, I’ve been provided with a template that looks something like this:

	leaderstats = {
		Wins = {0, 0, 0},
		Jump = {0, 0, 0},
		Rebirths = {0, 0, 0},
	},

Currently I’m aware that each index represents a “place”, but not much else. What do they actually mean? I’ve looked into the eternitynum module, but can’t find much on the bnum module. Any help would would be appreciated.

3 Likes

–Bump minimum characters needed

You need to separate them from the leaderstats because the leaderstats will be for display only. Then, you can create another folder to replicate the data to the client, like so:

local EternityNum = require(game.ReplicatedStorage.EternityNum)

local winsData = {1, 3} -- or 1_000

game.Players.PlayerAdded:Connect(function(player)
	-- Displayable leaderstats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("StringValue")
	wins.Name = "Wins"
	wins.Value = EternityNum.short(winsData) -- Converts winsData to a displayable string, "1k"
	wins.Parent = leaderstats

	-- Replicated Data
	local statsFolder = Instance.new("Folder")
	statsFolder.Name = "Stats"
	statsFolder.Parent = player

	-- Convert winsData to a valid EN if it's not already, then set the attribute as a string
	statsFolder:SetAttribute("Wins", EternityNum.toString(EternityNum.convert(winsData)))
	-- On the client side, use EternityNum.fromString() to convert it back to EN, so you can use it again
end)

Also, you can use the EternityNum type, which is {sign, layer, exp} , or the BigNum type, which is {base, exp} .

If you have any questions feel free to ask! :wink:

1 Like

I don’t quite understand this bit of code and what you mean in the comments. Can you rephrase?

Also, I have a leaderstat that is supposed to be the player’s jump power, and I also have to increment the player’s jump power to be the leaderstat. How would I incorporate this and eternitynum seamlessly? Also, how can I stop my leaderstat from being a decimal eg going 12.99 or 13.99. Sometimes it doesn’t round correctly.

Sadly, there’s no way to fix the decimal problem because it’s how computers calculate numbers., For the leaderstat, you just update the data and then update the leaderstat again like this:

local winsData = {1, 3} -- or 1_000
winsData = EternityNum.add(winsData, 5) -- 1_000 + 5
player.leaderstats.Wins.Value = EternityNum.short(winsData)

-- EN: A valid EternityNum Table
-- "Convert winsData to a valid EN if it's not already" means that toString only accepts EN.
-- If winsData is a number, like 1000, it will cause an error. To prevent this, we convert whatever it is to an EN.
-- On the client side, you can use EternityNum.fromString() on this attribute, to convert it back to EN, 
-- and then use EternityNum.short() to display it on a GUI or something like that.
player.Stats:SetAttribute("Wins", EternityNum.toString(EternityNum.convert(winsData)))

If you don’t know how to use a function, check the documentation inside the module.