Question about self

How does “self” exactly work? How does “self” know that it is “self”, despite never being defined in the code?

I have only used self in metatables, but I don’t think it can be applied to regular tables. What self essentially is an instance of a metatable passed as an invisible argument for functions that are like METATABLE:FUNCTION(), note the colon.

If this is confusing just tell me and I will whip an example up.

Its a reference to the space you are currently executing it in. It literally means ‘myself’, referring to the current ‘object’ its being called inside of.

Its talked about a little here

Alright, so, for instance, you’re writing code for a gamemode.

Let’s say your function is GM:Initialize. See the part before the :? GM? in that function, self = GAMEMODE.

Or in SWEP:Deploy, self = SWEP. Get it?

All self does is grab the object of the function you’re working in. I’m pretty sure that if you did self:whatever outside of a function, then it would either be nil or G.