Why won't some metamethods work properly?
Asked by
2 years ago Edited 2 years ago
I'm using metamethods for my "Number Instance" module to act like real numbers. Most of the metamethods are working fine, except for these three: __eq
, __le
, and __lt
. They are scripted the same way with __concat
, __add
, __sub
, __mul
, __div
, __mod
, and __pow
, but they won't work properly as expected. I tried printing them in a script and this is what they printed:
01 | local NumberService = require(game:GetService( "ReplicatedStorage" ):WaitForChild( "CustomServices" )):GetService( "NumberService" ) |
03 | local TestNumber = NumberService.new( 50 ) |
05 | print ( "My number is " .. TestNumber) |
13 | print ( tostring (TestNumber)) |
14 | print ( 50 = = TestNumber) |
17 | print ( 40 < = TestNumber) |
18 | print (TestNumber > = 40 ) |
This is the expected output to the lines that are not working
And this is the module script
001 | newNumber.__index = NumberInstance |
002 | newNumber.__concat = function (a, b) |
003 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
004 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
006 | if isNumberInstance 1 and isNumberInstance 2 then |
007 | return a.Number .. b.Number |
008 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
010 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
012 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
016 | newNumber.__unm = function (self) |
017 | return -(self.Number) |
019 | newNumber.__add = function (a, b) |
020 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
021 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
023 | if isNumberInstance 1 and isNumberInstance 2 then |
024 | return a.Number + b.Number |
025 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
027 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
029 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
033 | newNumber.__sub = function (a, b) |
034 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
035 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
037 | if isNumberInstance 1 and isNumberInstance 2 then |
038 | return a.Number - b.Number |
039 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
041 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
043 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
047 | newNumber.__mul = function (a, b) |
048 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
049 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
051 | if isNumberInstance 1 and isNumberInstance 2 then |
052 | return a.Number * b.Number |
053 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
055 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
057 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
061 | newNumber.__div = function (a, b) |
062 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
063 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
065 | if isNumberInstance 1 and isNumberInstance 2 then |
066 | return a.Number / b.Number |
067 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
069 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
071 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
075 | newNumber.__mod = function (a, b) |
076 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
077 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
079 | if isNumberInstance 1 and isNumberInstance 2 then |
080 | return a.Number % b.Number |
081 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
083 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
085 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
089 | newNumber.__pow = function (a, b) |
090 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
091 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
093 | if isNumberInstance 1 and isNumberInstance 2 then |
094 | return a.Number ^ b.Number |
095 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
097 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
099 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
103 | newNumber.__tostring = function (self) |
104 | return tostring (self.Number) |
106 | newNumber.__eq = function (a, b) |
107 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
108 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
110 | if isNumberInstance 1 and isNumberInstance 2 then |
111 | return a.Number = = b.Number |
112 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
114 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
116 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
120 | newNumber.__lt = function (a, b) |
121 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
122 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
124 | if isNumberInstance 1 and isNumberInstance 2 then |
125 | return a.Number < b.Number |
126 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
128 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
130 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
134 | newNumber.__le = function (a, b) |
135 | local isNumberInstance 1 = typeof(a):lower() = = "table" and a.Number ~ = nil |
136 | local isNumberInstance 2 = typeof(b):lower() = = "table" and b.Number ~ = nil |
138 | if isNumberInstance 1 and isNumberInstance 2 then |
139 | return a.Number < = b.Number |
140 | elseif isNumberInstance 1 and not isNumberInstance 2 then |
142 | elseif not isNumberInstance 1 and isNumberInstance 2 then |
144 | elseif not isNumberInstance 1 and not isNumberInstance 2 then |
148 | newNumber.__len = function (self) |
149 | error ( "attempt to get length of a number value" , 0 ) |