How could I use "self" in a script?

I have been confused with “self” in lua and have never used it.

What is “self” and an example on how i could use it?

4 Likes

I personally use self as a table name, mainly for a module. Something like this:

local self = {};
self.Get = function()
return false
end
return self;

I am not sure if that’s the best use of it, but that’s personally how I use it.

3 Likes

To my understanding its only really used in Module scripts. Self is a whole discussion on Object Oriented Programming but basically self is used to reference an object within its own code. Thats a simple way of putting it. Im not sure how much you understand about object oriented programming but if you dont know anything about OOP, its probably something you should take your time learning at a smooth pace.

2 Likes

This should help you:

3 Likes

Thank you! This showed me more and im starting to know it!

1 Like

self is like a variable which can be set to many things.

2 Likes

Like people already mentioned it’s mainly used in OOP but basically it’s just a shortcut to access an object’s table (data). There’s a few more instances you could use it for but this is the most useful case.

2 Likes

https://www.lua.org/pil/16.html

‘self’ is an implicit parameter used in Lua OOP.

3 Likes