What Is Self In A ModuleScript And How Does It Work?

Some module scripts have a self parameter, what is it? What does it do? Why do people use it?

local tab = {}

function tab:func1(n)
	self.hi = n -- self is just tab
end

tab:func1(10) -- tab:func1(10) is the same as tab.func1(tab, 10)

I see “self” as a reference to the table.

local MyModule = {}

MyModule.__index = MyModule 

function MyModule.New(User)
	
	local ModuleNew = {}
	
	setmetatable(ModuleNew , MyModule)
	
	ModuleNew .User = User   --This is defined as (Character in another script)
	
	return ModuleNew 
end

---
function MyModule:WhatsMyName()

print(self.User.Name) --Expected Output; "Character.Name" (LizoomHD in my case)

end)

Great question, I was also confused when first coding in lua. This is gonna be a little long, and I’m writing as fast as possible so hopefully what I write is understandable.

first let us look at how functions are called.

here is an example function:

local myModule = {}
function myModule.test(parameter1)
   print("testing")
   print(parameter1)
end

this function above can be called in two different ways.

  • myModule.test()
  • myModule:test()

Note the only difference is . vs : within the function call.

myModule.test() will run the function and pass no parameters.

myModule:test() however, will run the function and pass myModule as the first parameter. What ever preceds the : is what is passed as the first parameter, in this case being myModule.

so using that logic, these two function calls are equivalent:

  • myModule.test(myModule)
  • myModule:test()

Now for function definitions.
Look at the function from above again.

local myModule = {}
function myModule.test(parameter1)
   print("testing")
   print(parameter1)
end

we can re-write by using a : instead of a . within the function definition such as:

local myModule = {}
function myModule:test()
   print("testing")
   print(self)
end

Notice that the first parameter within the function definition is now gone. And instead of printing that parameter, we print self.

This function is (mostly) equivalent to the first one because when we use : within the function definition, it automaticly sets the local variable self as the first parameter passed.

if we were to call the function with myModule:test(), the first parameter passed is myModule. So with this function definition, inside the function the local variable self is automatically set to myModule.

The only reason this exists is for speed up code development and is also supposed to help people read and understand the code better as well (as long as you understand the concept), since we could always just define self manually.

It gets a little more advanced when looking at object oriented programming (OOP), that in itself is a large topic, but self is used a lot even outside of OOP, when working with Roblox module scripts.

1 Like

self is just a way of getting what was called with : and .

local funcs = {}
local t = setmetatable({"hi"}, {
        __index = funcs
    })

function funcs.Print(self)
    print(self) --self is not automatically defined in dot syntax, so make sure to define it in the first parameter
end

function funcs:Print()
    print(self) --self is automatically defined in colon syntax and is set to the table 't'
end

t:Print() --does the same thing as t.Print()
t.Print() --does the same thing as t:Print()

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