Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Adding math.huge to player leaderstats?

Asked by 8 years ago

Hi I was trying to make the player buy a game pass then when they buy it then they get math.huge added to their balance. My script isn't working can you help?

local passId = 281365697
local plr = game.Players.LocalPlayer
function click()
        if game:GetService ("GamePassService"):PlayerHasPass (plr, passId)then
    plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + math.huge
else
    game:GetService("MarketplaceService"):PromptPurchase(plr, passId)

end
end
script.Parent.MouseButton1Click:connect(click)

0
Is `Cash` an IntValue or a NumberValue? BlueTaslem 18071 — 8y
0
IntValue docrobloxman52 407 — 8y
0
add some parenthesis around math.huge. HungryJaffer 1246 — 8y

2 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

You're dealing with a problem of the way computers represent numbers.

Number Representation

pi is an irrational number. 3.14159265359... If you wanted to store all of the digits -- you can't. That would require too much memory1.

There are two main ways that numbers are approximated by computers to make dealing with them possible.

Fixed Point

Fixed-point numbers essentially fix the decimal place at a particular point. Integers are a good example:

5 is OK, 6 is OK, 15 is OK, 16 is OK, but if I want 15.5, I don't have enough precision to do that, and will have to approximate it as 16.

Other systems might fix it at 100s -- 1600 or 1700 is fine but 1676 has to be approximated as 1700.

You could also fix it at 100ths -- 13.34 is fine, but 3.141519265 has to be approximated as 3.14.

These numbers are extremely easy for computers to interact with. They have obvious limitations, however. Lua 5.3 adds support for integers, but Lua 5.1 (what ROBLOX uses) doesn't have them.

IntValues store fixed point numbers -- I will come back to this.

Floating Point

Floating-point numbers try to solve the precision problems of fixed-point numbers with a scientific perspective.

If you've taken science classes, you may have heard of "significant figures". When your'e taking measurements, only the first few digits really matter:

Even though 16000000 is 8 digits long, it's clearly less precise than 1.234. The 16000000 is probably only accurate to the nearest million, which is 10% of the number, while 1.234 is accurate to the nearest thousandth, which is 0.1% of the number.

Another way to think about this is to write all numbers in scientific notation: 16000000 is 1.6 * 10^7 and 1.234 is 1.234 * 10^0.

Floating point numbers take this approach to viewing numbers. They mostly store the first few digits, and they also store the exponent, which is basically the total number of digits.

These numbers are much more complicated to interact with. Adding is no longer easy (or associative) because the decimal places don't necessarily line-up: e.g., 1.23 * 10^0 + 5.67 * 10^1 is ...?

Because these operations are more complicated, the standard implementations add a few "special" numbers:

  • -0
  • -infinity
  • infinity
  • not-a-number or NaN

-0 is just -1 * 0.

-infinity is -1 / 0. -infinity + x is still -infinity.

infinity is 1 / 0. infinity + x is still infinity.

NaNis the result of really poorly defined operations, like 0 / 0 or infinity - infinity. Any operation involving a NaN produces a NaN.

Lua's numbers are floating-point numbers.

Lua's math.huge is the floating-point infinity.

Infinity and Overflow

Floating point numbers deal gracefully with big and small numbers alike, since you just need to care about the first few digits.

However, fixed-point numbers have an issue. They really easily "run out" of numbers. While floating point numbers can easily hold 10^100, fixed-point can't because that would mean they need space for each individual number from 0 up to there.

This means there is a largest fixed-point number.

Try this code out:

local v = Instance.new("IntValue")
v.Value = 2147483647
print(v.Value) --> 2147483647
v.Value = v.Value + 1
print(v.Value) --> -2147483648

You must be thinking, what? How can this be?

2147483647 is the largest int value2. Adding anything to it will have to make it smaller, since there are no numbers after that. If you add 1, you get to the smallest number.

What do you do?

One option is to use NumberValue instead of IntValue. That will let you just use math.huge and go on your way. You'll have to make sure you were never relying on the IntValue to make a non-integer into an integer, though.

Another option is to use 2147483647 instead of math.huge. This will look really strange to your players, and will cause problems if you ever add by any amount. A more user friendly way to solve this would be to pick a huge, round number, like 500000000 instead. It's really unlikely this will be exhausted, but it is a natural number and it will be as good as infinite.


  1. This is when storing it strictly as a single number. You can store it symbolically, but you lose the ability to do computation (e.g., get 1 + pi as as 4.15169265359...

  2. It's 2^31 - 1

0
*claps *slowly moves mouse to remove answer.. HungryJaffer 1246 — 8y
0
I already explained this to him in the chat, but amazing answer c; HungryJaffer 1246 — 8y
Ad
Log in to vote
-2
Answered by
Mokiros 135
8 years ago

In line 4 you forgot to check bool if player have gamepass.

local passId = 281365697
local plr = game.Players.LocalPlayer
function click()
        if game:GetService ("GamePassService"):PlayerHasPass (plr, passId) == true then
    plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + math.huge
else
    game:GetService("MarketplaceService"):PromptPurchase(plr, passId)

end
end
script.Parent.MouseButton1Click:connect(click)
2
`== true` is unnecessary and bad style. `PlayerHasPass` already returns a boolean. This just makes the script slightly worse. BlueTaslem 18071 — 8y

Answer this question