What is self? (in the simplest explanation)

Hello everybody,

I want to know what self is, and I want it in a simple explanation since most explainations are really complicated and don’t make sense to me. I know that it is used in a Module, but that’s it.

1 Like

self is used within modules to easily run other functions

Can I have an example?

aaaaaaaaaaaaaaaaaaaaaaaaaa

Self is used in Object Oriented Programming to define the object a method is referred to as its first parameter.

When you call Instance:Destroy(), you’re not inserting any parameters. Within the Destroy method, self is defined as the Instance you’re calling the method on.

Instance:Destroy()

-- Destroy:
function x()
	self.Parent = nil
	-- self is the Instance you called the method on
	return x
end

-- it's the same as in:
x.Destroy(Instance)

function x.Destroy(Instance)
	Instance.Parent = nil
	return x.Destroy
end

Except, Instance is a class, so you can’t name it that way. I’m on mobile so I don’t care.

2 Likes

On the most simple level, self is what the function was called on if the function has :, of course this is combined with __index to basically create classes.

local something = {}

function something:ChangeSomethingOnSomething()
    print(self == something)
    self.someValue = 5
end
6 Likes

Just think self as “myself”. When I do the function (method) on myself… I declare my value as this…

1 Like

While I was testing out @mniao’s script, that printed true, so would that mean self equals the module?

So that would mean, Destroy’s function is kind of like this?

function Instance:Destroy()
   self.Parent = nil
end

thisisainstance:Destroy() -- thisisainstance gets parented to nil

Because,

function anythingthatwascalledhere:A()
   -- self == anythingthatwascalledhere
end

Yes, on that case we’re only using a table so self is always gonna be equal to it, but here’s how you would normally use self.

local class = {}
class.__index = class --if nothing is found under the table then search on class table

function class.new()
    local self = setmetatable({}, class) --create a new table and set the new table's metatable to class
    return self
end

function class:ChangeSomethingOnSomething()
    print(self == class) --false if called on newObject
    self.someValue = 5
end

local newObject = class.new()
newObject:ChangeSomethingOnSomething()
--[[
Since newObject doesn't have a function called "ChangeSomethingOnSomething",
it will instead search on class, on which case self will be equal to newObject inside the function since
that's what we called the function on.
]]
1 Like

self is self. Referring to the own scope of the table. self by default is nil, until it is moved in a specific table context/scope. Use cases involve interacting with “siblings” in the table.

-- This is the table
local randomTable = {}

-- Function Foo, which prints the param it gets
function randomTable:Foo(message) -- implicit self = randomTable, sugar syntax
    print(message)
end

-- Function Bar, calls self:Foo("Bar!"), subsequently printing "Bar!"
function randomTable:Bar()
    self:Foo("Bar!")
end

-- Calling the function Bar
randomTable:Bar()
2 Likes

I’ll mark this as the solution, since it was the one that started making me understand what self is. Thanks everyone who helped!

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