Confused about Self

Hi

I simply do not understand (Self) or What is the purpose of (Self)?
i have seen (Self) in alot of scripts and I’ve tried looking for it and I still don’t get.

Any help would be appreciated!

Did you do research? This has already been asked. If you have, which parts in particular do you not understand?

yes and i still don’t understand What is the purpose of using Self Generally

I am not the OP but I am adding myself into this, I know how to use self but I wasn’t able to figure out why? What are actual uses of self (except metatables).

So that you have a reference to the table that the function is contained within.
In OOP, you can have multiple tables of the same type that all do the same thing. They all have the same function in them. When you call the function, the function doesn’t know which table it is inside of. Self fixes that.

tab = {}
tab.var = "hi"
tab.func = function(self) print(self.var) end
tab:func() -- automatically sends self as the first parameter when using a colon

It is object oriented programming, and if you want to really learn why you need it or how exactly it all works you should look into an object oriented programming tutorial either in Lua or a different language. Object-oriented programming is all but useless on Roblox. It has its place but it is never necessary and it tends to complicate things. People seem to get over excited about it when they first learn it because it is a complicated thing to learn and they are proud that they did it. But 95% of the times when you can use it, you probably shouldn’t

2 Likes

self typically is used in Object Orientated Programming from most instances I’ve seen where I and others have used it, it’s used with the : operator in module functions, self is just the dictionary that ran the : function

Reading about how Object Orientated Programming works should be an idea as to how is self used

And you can scroll through the replies of this post to help you get a brief understanding of what the keyword is specifically

Edit: No I was just dumb, there’s this as well, would recommend this one as this is where I understood OOP

3 Likes