What does self refer to in meta tables?

Hello,
I have just recently started meta tables and was wondering what self refers to when using meta tables. Is it referring to the meta table itself? If so what is that useful for then?

local table = {"Something","Useful"}

setmetatable(table, {
   __newindex = function(self)
          print(self) -->> {...} -> "Something","Useful"
   end,
})

In metamethod functions, the first argument is the table the metamethod is being fired from

If you want to access the metatable within your metamethods, use this

local mt = {}

function mt:__index()
  print(self) --> table that fired the metamethod
  print(mt) --> the metatable
end

local t = setmetatable({}, mt)
local x = t[1] -- prints the table and metatable

self isn’t limited to metatables it has more to do with instance methods than metatables. Even if you don’t use metatables in a ModuleScript you will still have self available inside instance methods. self is a reference to the table that represents the module and it is passed automatically for instance methods (defined with a : ).

-- ModuleScript MyModule located in ReplicatedStorage
local module = {}

function module:InstanceMethod()
  print(self) -- prints module
end

function module.StaticMethod(self)
  print(self) -- prints whatever is passed
end

return module

-- Consumer script

local mod = require(ReplicatedStorage.MyModule)

-- these are effectively the same
mod:InstanceMethod()
mod.StaticMethod(mod)

Wouldn’t this print the address in memory at which the table is currently located?

Instead of referring to executed metamethods as them having been fired, invoked would be the more appropriate term as to not cause any confusion.

Your script would also error. This was likely what you intended.

local mt = {}

mt.__index = function(self)
	print(self) --> table that fired the metamethod
	print(mt) --> the metatable
end

local t = setmetatable({}, mt)
local x = t[1] -- prints the table and metatable

Or with the colon operator.

local mt = {}

function mt:__index()
	print(self) --> table that fired the metamethod
	print(mt) --> the metatable
end

local t = setmetatable({}, mt)
local x = t[1] -- prints the table and metatable