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)
You're dealing with a problem of the way computers represent numbers.
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 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 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
is just -1 * 0
.
-infinity
is -1 / 0
. -infinity + x
is still -infinity
.
infinity
is 1 / 0
. infinity + x
is still infinity
.
NaN
is 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.
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.
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.
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)