Class returning nil on weapons system

Hi,

So currently I’m trying to script a weapon system utilizing OOP (or just Object Oriented Programming). But while creating a class for a weapon, the Attributes meant to be used for self is returning nil values instead.

While printing the contents, it returns {} instead of the Class.

Script Contents / Hirearchy Module:
function utl.new(Tool: Tool, Config: Configuration)
    local self = setmetatable({}, { -- creates self
	    __index = item;
	    __newindex = function(t, i, v)
		    if not Config:GetAttribute(i) then return end
		    Config:SetAttribute(i, v)
     end;
    });

    self.Tool = Tool; -- basic stuff
    self.Handle = Tool.Handle;
    self.Config = Config;

    for i,v in Config:GetAttributes() do -- Point of error / where everything else is added
	    self[i] = v;
    end

    return self; -- suppsoed to return self and its contents
end

Server:
local ReplictedStorage = game:GetService(“ReplicatedStorage”);

local Mod = require(ReplictedStorage.Mod);

local Tool = script.Parent
local Config = Tool.Config

local Weapon = Mod.new(Tool, Config); -- Class created here
print(Weapon); -- where contents check is
Weapon:Setup() -- unimportant for now

What could be the issue here?

1 Like

Did you try using a different variable aside from ‘self’?

That wouldn’t do anything, the variable would just be self under a different name.

1 Like

i think you had to do something like

setmetatable({}, utl)

Forgot to say that I found a solution for this topic.
I turns out __newindex screwed with how stuff in tables were added to it, so after reordering some code and functions, it was fixed, and ready for use.

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