0

so i made 2 module scripts

here's the code:

local base = {}
base.__index = base

base.Number = 5

function base:New()
local t = setmetatable({}, self)

 t.__index = t 
return t
end

 return base

second:

  local base = script.Parent.Base


 local child = require(base):New()

  return child

this one is normal script:

  local child = require(script.Parent.Child1)

   print(child.Number)

and i get this message after i try executing it

stack begin script 'ServerScriptService.Script', Line 1 Stack End

it should've executed "5" but its not working and i don't know why

  • What are `script.Parent.Child1` and `script.Parent.Base`? My first guess would be that require fails for the last script. – Luke100000 Mar 26 '22 at 10:19

1 Answers1

0

In your Base class, you set the __index metamethod to the base object, then in the constructor you say setmetatable({}, base) these two lines mean that when someone asks for a property on the empty table, that it should be found on the base object instead. But they you overwrite that behavior by setting the __index metamethod again. This removes the relationship between the new object and its base class.

So to fix this, just remove that line in the constructor.

function base:New()
    local t = setmetatable({}, base)
    return t
end
Kylaaa
  • 6,349
  • 2
  • 16
  • 27