How would I use "self" and why is it useful?

Looking at some posts about self, it still confuses me for some reason, I was hoping to get a Better understanding of it?

Thanks.

got it?

self is like a variable name. Since I never use that command, I don’t know so much about “self”

Self is used inside of module scripts. It is a variable which is used inside of OOP programming which is basically whatever the first passed into the modal. I would recommend learning about OOP programming if you wish to understand it better.

Here is a useful post made about it: What is self and how can I use it? - #2 by WallsAreForClimbing

self alone just acts like a normal variable, with the only difference that the text is highlighted.

In functions, these are used for functions that are defined with : inside tables, which allows the self variable to be used instead of the first argument.
However, this would require the functions to be called with : as well in order to get the most efficiency of it, as calling it normally will use the first argument as the self argument.
This is called as Syntax Sugar, since you’re only making the code more cleaner to read.

local object = {}
function object:Method(arg1)
   print(self == object,arg1)
end

object.Method() --> false, nil
object:Method() --> true, nil

object.Method("Hello world!") --> false, nil
object:Method("Hello world!") --> true, "Hello world!"

It’s really useful in Object Oriented Programming, since this can be used to update various objects while using the same function to do any action to the object.

3 Likes

I have already looked at this, I’m still confused about it

What confuses you with that? It is quite clear:

As you can see this, self is the exact same then what comes out of the method.

1 Like

I just don’t understand it, every time I use it it always prints:

20:32:20.073   â–Ľ  {
                    ["A"] = "function",
                    ["E"] = "function"
                 }  -  Server -

Random letters as functions

Mind providing context to this? Like show the code.

Same thing as this

It may just be the function I’m using

How are you calling the method?

The 2nd Example because appearently self is already defined

Looking at this and testing it, it does not work, it always prints false or nil

Ill try to explain in a simple way:

Im sure your probably a bit familiar to OOP, and thats what self is used for.
It’s a bit tricky to explain, so I will give you a example:

local TestModule = {}

 function TestModule.new(Data)
local Table = setmetatable(TestModule,Data)
Table.Data = Data
print(self.Data)
return Table
end

function TestModule:ChangeData(NewData)
self.Data = NewData
end
return TestModule

All we are doing in the code above is creating two functions. However, the way we create them is a bit different. For the first function, all we are doing is creating a metatable and setting it to the main table. However, when we create this function we are using the . operator. Usually it doesnt quite matter, however for OOP it does.

When we create an OOP object, we create it like this:

local TestModule = require(game:GetService("ReplicatedStorage").TestModule)

local Object = TestModule.new({"Coins = 100", "Level = 10"})
Object:ChangeData({"Coins = 50", "Level = 12"})


We dont really need this information though, we need to talk about self more.
All self is really, is a variable. Self, is basically the object your referring to. You see, when we create the “new” function, we are returning a metatable. With this returned metatable, we are calling the “:ChangeData” function. Since this has a : operator, Luau will automatically define the self variable as the metatable. With this, we can just get the information we previously stored, and use or alter it.

One more thing:

Lets say we have these two functions:

function TestModule.TestSelf()
	print(self)
end

function TestModule:TestSelfAgain()
	print(self)
end

As you can see, the first one is syntax highlighted. This is because self CANT exist because we are creating the object. However, on the second one self CAN exist because we are not creating an object, but adding a “method” to it. (self WONT exist if you CREATE the object using the : operator. You must use the . operator.)

I hope this helped, and let me know if you have anymore questions!

12 Likes

Dont worry! What your doing in your code is creating a table with the name self. The thing is, self isn’t really a “reserved” keyword like “if” or “else” so you can create variables with it. While you create the variable, it still acts exactly like a table would. So infact, if you changed all the “selfs” do a different name, it would work aswell!

I dont exactly understand what your trying to ask, so if i didn’t answer it please let me know!

Here’s an example:

local Module = {}

function Module:ChangeValue(NewValue)
	-- You MUST use : in your function name
	-- or else you have to parse self as a 
	-- parameter
	
	self.Value = NewValue
end

return Module

All in all, self just allows you to access the table.

Hi, Sorry for the late response

This makes me feel stupid and oblivious to what these functions do.


Understandable


Repeat in Text


I Actually don’t know what a metatable is


I Get a bit confused here.

Thanks for pointing those things out!

Just wanna clarify that I dont think they’re called . Operator and : Operator, but i might refer to them like that.

metatables are a lot harder to explain, and I do feel like you should reach out to a different source to learn about them, like this:

They’re difficult and personally, I dont fully understand it but hopefully this resource can help you on the basics.

For the part you dont exactly get let me rephrase because I believe i overcomplicated it.

like I said previously, there are two ways you can make a function. (using :, and .)
When you use oop, you usually have a function to create the object itsself. Most people will create the function using . (However, I dont exactly know if you need to, but I believe its just the best scripting practice)

Anyways, when we create this object, it is impossible for the computer to know what self is. This is because there is no self. Infact, the object itsself is quite frankly what self is.

when we call a function upon that object using :, there is basically an invisible first parameter that creates the self variable. This variable is just the object thats calling it.

Let me give you an example:

--// Previous example:
function TestModule.new(Data) -- This function creates the object.
	local Table = setmetatable(TestModule,{}) -- creates a metatable.
	Table.Data = Data
	return Table -- returns that metatable so you can use it when you create the object
end

function TestModule:ChangeData(NewData) -- this uses :, so it automatically declares self.
	self.Data = NewData -- self is just the object thats calling :ChangeData
	print(self.Data) -- we print it
end

In a server script:

local TestModule = require(game:GetService("ReplicatedStorage").TestModule)

local Object = TestModule.new({"Coins = 100", "Level = 10"}) --// Here we create the object. Since It gives the returned value which is just a metatable 
Object:ChangeData({"Coins = 50", "Level = 12"}) --// We call our method. since we use the : operator, self is available and can be used to edit/ store data inside the metatable. Self is just "Object"

Hope this helps and let me know if you have more questions!

3 Likes

Well, thank you, I’ll be sure to look at all the info!

1 Like

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