Cyclic tables + OOP issues

I’m trying to use CollectionService to tag an object. My current setup is as follows:

local testObject = Object.new();
CollectionService:AddTag(testObject , "Tag");

local Object = {};

Object.__index = Object;

function Object.new()
	
	local self = setmetatable({}, Object);
	
	self.X= BlahBlah;
		
	return self;
	
end

I’m getting the error ‘tables cannot be cyclic’. I understand this is because I am referencing the table with Object.__index = Object. My question is, is there any way I can reformat my code to allow for this? Any help would be great as I’m stuck and the alternative method would take a decent amount of work compared to a possible solution to this.

1 Like

You can instead have a separate table be the metatable.

local mt = { __index = Object }

Also you cannot give a tag to a table since it is not a Roblox Instance.

You aren’t supposed to use CollectionService with non-Roblox instances. Everything you’re doing right now is fine as it is and can be left alone except for that one thing which is butchering your system. Consider a custom tagging implementation of some kind to get around this.

Refer to CollectionService for more information. Notice that the parameter required is Instance, meaning the absolute Roblox base class. Your constructor doesn’t return an actual instance recognised the same way, it’s just a table.