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

Why is my class instance nil?

Asked by
Roger111 347 Moderation Voter
4 years ago
Edited 4 years ago

OOP HELP... I have created my own class Point and I have 2 simple methods in it Point:print() and Point:getMagnitude(). Here is code:

01local Class = require(game.ReplicatedStorage.Class) -- Template (see note at bottom)
02 
03local Point = Class:extend()
04 
05function Point:new(x,y,z)
06    self.X = x or 0
07    self.Y = y or 0
08    self.Z = z or 0
09end
10 
11function Point:print()
12    print(self.X,self.Y,self.Z)
13end
14 
15function Point:getMagnitude()
16    return math.sqrt(self.X^2+self.Y^2+self.Z^2)
17end
18 
19return Point

My problem is when I run some server code that tests the class I get this error: ReplicatedStorage.Point:12: attempt to index nil with 'X' I don't see any typos, and I do have a constructor method that should give me self... Help me! :( Server Code:

1local Point = require(game.ReplicatedStorage.Point)
2local pointA = Point(3,4,5)
3pointA.print()
4local pointAMag = pointA:getMagnitude()
5print(pointAMag)

Summary: self is nil when it should not be...

Line 1 Documentation: https://github.com/rxi/classic

1 answer

Log in to vote
0
Answered by
Roger111 347 Moderation Voter
4 years ago
Edited 4 years ago

ANSWERED: By @TimScript on the discord server

This line: pointA.print()

Needed to be: pointA:print()

Ad

Answer this question