How can incredibly large values be created and modified? (Similar to Miner's Haven)

Hello, I am trying to create a currency system for my game, but I can’t seem to efficiently store and create large values. Currently, I am using an implementation of the BigNum library from RoStrap, but it is slow when adding values past 1 quadrillion. In Miner’s Haven, it seems as if the server handles values well over a decillion with no issue. How exactly was this accomplished without lagging out the server?

3 Likes

You may want to check this out

Thanks for the reply. The library that was recommended in that thread is the same library that I am using now (albeit ported for Roblox use). My issue is that when updating the values, the server lags immensely. I am unsure if BigNum is the way to go when dealing with large values.

Simple addition really shouldn’t cause this, even with big numbers. How reliable are the performance tests you’ve performed? I suspect there is another culprit at play.

The reason Roblox has this limitation on the value on an integer is because of how the Lua enterpreter itself works.

When you create a variable, Lua dynamically assigns memory to it. In C/C++, there is a memory limit on how large this can be. If you provided a number higher than that (and Lua allowed it and didn’t error), then the number would actually underflow and go to the minimum value.

But, anyway, I’m getting off topic.

Could you try storing the number as a string and manipulating it from there. I’m not sure how that would be performance wise, but it should remove the limit on size (the Lua limit for string sizes is much larger than the one for integers).

This would be the best way to do it. Create a custom function for adding, subtracting, dividing, multiplying etc.

From what I know though, Miners Haven just natively uses NumberValues or IntValues and doesn’t do anything special.

This is just the standard size of integers. Lua uses doubles, but it has nothing to do with the integer limit.

This wasn’t an issue I had when I ported the same exact module. You must have done something wrong.

That aside, I recommend checking out RoStrap. A lot of people seem to like it, and it includes BigNum.

1 Like

So I did a quick test, and ran this in studio:

local n = 0

game:GetService("RunService").Heartbeat:Connect(function()
	
	for i = 1,20 do
		n = n + math.random() * 1e30
	end

	if math.random(1,15) == 1 then
		print(n)
	end
end)

And the script performance window had it at an activity of 0.000%, which is to say, completely negligible. So what’s wrong with just using floating point numbers instead of integers? There is no possible need to keep 33 digit precision for a player with a decillion moneyz.

3 Likes

Instead of using values since it has a “max number”, why not use a table or a variable instead for using these?

I’m pretty sure that’s the same method the Miner’s Haven used.

BigNum from RoStrap was perfect for my needs. I found the obvious problem that was causing the short lag spikes to be that I was adding regular base 10 numbers to my BigNums in stead of adding BigNums together. Thanks for the help everyone.

Glad BigNum from RoStrap worked out!

I want to point out that Lua 5.1 (and Roblox’s version of it) always uses doubles for all numbers. These can represent integers and floating point numbers. An integer in Lua 5.1 is a double, just the same as any floating point number. Google “Double-precision floating-point format” for more information.

Also note, there are some values which Roblox holds as an integer, which get converted to a double when you read them in Lua. For example, ScreenGui.DisplayOrder is a 32 bit signed (two’s complement) integer on the C++ side, but when you access it in Lua you will see an equivalent double/Lua number type. (Although the number itself holds negative values, the C++ engine keeps you from setting it to an integer below 0. That, or they just enforce the range [0, 2^ 31 - 1])

2 Likes