OOP Metatables promblem

Today, I was trying to learn more about oop but I ran into a very confusing error.

module Script

local module = {}
module.__Index = module

function module.New()
	local self = setmetatable({},module)
	
	return self
end

function module:Hello()
	print("hello")
end

return module

script

local Module = require(game.ServerStorage.ModuleScript).New()

Module:Hello()

the error I’m getting ServerScriptService.Script:3: attempt to call a nil value

You don’t need to define self, self is getting the function itself.
I think renaming it should work.

i try renaming it but still gave me same error

use

module.__index = module

__index should be all lowercase.
Everything else looks correct.

1 Like