Object oriented programming failing

I attempted to store an instance inside a table in a module script. Apparently this doesn’t work, someone please help me.

--tile is a node group object
--contains up to four nodes
local oceanTile = game:GetService("ServerStorage"):WaitForChild("OceanTile")
local Tile = {}
Tile.__index = Tile

function Tile.new(node1,node2,node3,node4)
	local self = setmetatable({}, Tile)
	self.__index = self	
	self.nodes = {node1,node2,node3,node4}
	for i = 1, #self.nodes do
		self.nodes[i]:addTile(self)
	end
	self.plane = oceanTile:Clone()
	self.plane.Parent = workspace.oceanTiles
	return self
end
function Tile:getPosition()
	return self.plane.Position
end
function Tile:getNodes()
	return self.nodes
end

function Tile:setPosition(position)
	self.plane.Position = position
end

function Tile:setSize(size)
	self.plane.Size = size
end

function Tile:setCFrame(cfram)
	self.plane.CFrame = cfram
end
function Tile:getCFrame()
	return self.plane.CFrame
end
function Tile:containsNode(node)
	for i =1, #self.nodes do
		if self.nodes[i] == node then
			return true
		end
	end
	return false
end

function Tile:color()
	self.plane.BrickColor = BrickColor.random()
end

return Tile

Is there any errors?

P.S. I like the look! :sunglasses:

Thank you. The error comes when attempting to reference any of the properties of the “plane” outside of the initial Tile.new() call. From then on self.plane is nil.

Hey, first off, Code Review (#help-and-feedback:code-review) is meant for feedback on code that WORKS. Since your code does not work, this is more acceptable in #help-and-feedback:scripting-support, you can contact a Community Editor to move it for you.

Okay, so I think you genuinely just had a mishap, Remove the line that says: self.__index = self
You wouldn’t need that because Tile’s index is already set to itself, setting it again is redundant and will cause errors

1 Like

Caught this, thanks for the feedback! I’ve recategorised this post over to Scripting Support.

1 Like