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
3 years ago
Edited 3 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:

local Class = require(game.ReplicatedStorage.Class) -- Template (see note at bottom)

local Point = Class:extend()

function Point:new(x,y,z)
    self.X = x or 0
    self.Y = y or 0
    self.Z = z or 0
end

function Point:print()
    print(self.X,self.Y,self.Z)
end

function Point:getMagnitude()
    return math.sqrt(self.X^2+self.Y^2+self.Z^2)
end

return 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:

local Point = require(game.ReplicatedStorage.Point)
local pointA = Point(3,4,5)
pointA.print()
local pointAMag = pointA:getMagnitude()
print(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
3 years ago
Edited 3 years ago

ANSWERED: By @TimScript on the discord server

This line: pointA.print()

Needed to be: pointA:print()

Ad

Answer this question