What is the purpose of "self"?

I was recently coding a module, and I noticed a keyword called self. I saw some of my friends use self as a variable when they coded a module. They also used it in functions. what is the purpose of self?

I was looking through the developer forum and the developer hub, but I didn’t find any solutions.

There was someone that made an anticheat module like this.

Anticheat example

This text will be hidden

local self = setmetatable({ClassName = "CharacterBind"}, CharacterBind)
	
	self.Hooks = {}
	self.Character = character
	self.Player = PlayersService:GetPlayerFromCharacter(character)
	self.Torso = torso
	self.RootJoint = self.Torso:FindFirstChild("RootJoint") or character.LowerTorso.Root
	self.Humanoid = humanoid
	self.DiagnosticsFolder = character.FJsMovementAnticheat
	self.OnPunishment = self.DiagnosticsFolder.OnPunishment

	self.LastCFrame = self.Torso.CFrame
	self.LastGoodCFrame = self.Torso.CFrame
	self.LastGroundedAltitude = self.Torso.CFrame.Position.Y
	self.SpeedHeat = 0
	self.FlightHeat = 0
	self.VelocityHeat = 0
	self.IsBeingPunished = false
	self.CriticalCharacterComponents = {
		self.Humanoid,
		self.Torso,
		self.Torso:FindFirstChild("RootJoint") or character.LowerTorso.Root,
	}

any help is appreciated

self refers to the implicit variable returned when you define a function using method syntax:

local t = {}

function t:method()
  print(self) -- self here is t
end

It derives from the calling syntax t:method() where t will get passed as self. Its effectively a different way of calling t.method(t)

Here though, self is just being used as a regular old variable, nothing special about it. idk why linters even lint self when its nothing special outside of methods

2 Likes