Oop script somehow throwing up a whole diffrent table?

i followed the roblox oop tutorial and one of my scripts worked as fine, then i tried to make another system following the first oop script i made but now it doesnt work?

code:

constructor

local meleeConstructor_module = {}
meleeConstructor_module.__index = meleeConstructor_module



--[Constructor Functions]--
function meleeConstructor_module.new(tool, hitbox : BasePart, model : Model)
	local meleeData = {
		toolz = tool
	}
	
	local self = setmetatable(meleeData, meleeConstructor_module)
	
	return self
end

function meleeConstructor_module:remove()
	warn(self)
end





return meleeConstructor_module

weapon creator

local linkedlander_behaviour = {}
linkedlander_behaviour.__index = setmetatable(linkedlander_behaviour, meleeConstructor_module)



function linkedlander_behaviour.new(tool : Tool)
	--[Tool]--
	--[[local new__weapon_model = weapon_model:Clone()
	local hitbox = new__weapon_model:FindFirstChild("Weapon_Hitbox")
	
	
	
	--[Character Receiving]--
	local received_player = players:GetPlayerFromCharacter(tool.Parent)
	local received_character = received_player.Character
	
	local character_objects_folder = received_character:FindFirstChild("Character__Objects")
	local tool_folder = character_objects_folder:FindFirstChild("Tool")
	
	new__weapon_model.Parent = tool_folder
	hitbox.Parent = tool]]--
	
	--[Weapon Construction]--
	local constructed_melee = meleeConstructor_module.new(tool)
	local self = setmetatable(constructed_melee, linkedlander_behaviour)
	
	
	return self
end





return linkedlander_behaviour

output when checking from the constructor or the weapon constructor

{
                    ["__index"] = "*** cycle table reference detected ***",
                    ["add_behaviour"] = "function"
                 } 

also somehow the __index is part of the table’s functions or its just part of the table for some odd reason

It kind of looks like linkedlander_behaviour is a subclass of constructed_melee. If my assumption is correct, then you’ve messed up on the constructor of linkedlander_behaviour. self should be setmetatable(constructed_melee.new(), linkedlander_behaviour) (note that I’m using the constructor function for constructed_melee instead of constructed_melee itself). If constructed_melee’s constructor needs any arguments, you’ll want to put those in as well.

I think the reason you got the output you got is because “setmetatable(constructed_melee, linkedlander_behaviour” will use the returned value of constructed_melee as the table to set a metatable to. Since module scripts return a table composed of the functions they contain, you’ve basically told Roblox to set a metatable to the functions of constructed_melee instead of the OOP object.

I understand that you did print(linkedlander_behaviour) (or something like that, maybe by mistake). In that case, that message is normal. Instead of the message "*** cycle table reference detected ***", it would print the same table over and over again because you’re doing linkedlander_behaviour.__index = linkedlander_behaviour. But that would be a problem, so it’s better that it shows that message. The code is fine.

i printed the self because for some reason i cannot access the toolz variable, becuase it has the __index and the remove function thing

i suspect the index is broken or something since its reprinting the module table as well?

i tried remaking the melee constructor as a single class for now and the self is still printing the module functions instead of the melee data’s table

how do i solve it?

local meleeConstructor = {}
meleeConstructor.__index = meleeConstructor



function meleeConstructor.new(tool : Instance)
	local melee_data = {
		Tool = tool
	}
	
	local self = setmetatable(melee_data, meleeConstructor)
	
	
	return self
end


function meleeConstructor:Check()
	warn(self)
	warn(self.Tool.Name)
end




return meleeConstructor

output:

{
                    ["Check"] = "function",
                    ["__index"] = "*** cycle table reference detected ***",
                    ["new"] = "function"
                 }

nevermind i found that i was calling the function through the module instead of a variable

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.