OOP, __index with __newindex?

I’m trying to create an OOP module, but I would like CFrame and Size to be fired with __index and __newindex, but I’m having difficulties implementing __index with both the module dictionary AND the CFrame & Size values.

Any solutions?

What exactly are you trying to do with CFrame and Size? I’m quite new to metatables but i’ll try.

1 Like

Do you mean when Size and CFrame are changed, an event fires?

local part = workspace.Part 
local meta = setmetatable({}, {
    __index = function(tbl, index) 
    if index == 'Size' then 
            print('Size!') 
        elseif index == 'CFrame' then 
            print('CFrame!') 
        end
        return part 
    end;
    __newindex = function(tbl, index, value)
     if index == 'Size' then 
            print('Size!') 
        elseif index == 'CFrame' then 
            print('CFrame!') 
        end
        return part
    end;
})
meta.Size = Vector3.new(1,1,1) --> Size!
2 Likes

Yes, I’m trying to do what @7z99 is has right now, but I also want the metastable to be OOP’ed, or __index’ing a table called ‘MainModule’

Gotcha.

So inside of the empty table, we can create an event and use the event’s event using the table argument returned in the meta table.

local meta = setmetatable({[‘SizeChanged’] = Instance.new(‘RemoteEvent’).Event}, {
    __index = function(tbl, index) 
    if index == 'Size' then 
            print('Size!') 
        elseif index == 'CFrame' then 
            print('CFrame!') 
        end
        return part 
    end;
    __newindex = function(tbl, index, value)
     if index == 'Size' then 
            tbl.SizeChanged:Fire()
        return part
    end;
})

meta.SizeChanged:Connect(function()
    print('Size changed!')
end)

meta.Size = Vector3.new(1,1,1)

Sorry for weird formatting, I’m on mobile atm. Also sorry if it errors, not able to test atm.

1 Like

Oh alright, seems like it might work. Thanks.

1 Like