Confused about self

I was wondering what I should set as self. In other words, I know what self means, I know how to use it, but I do not know what to make it.

If someone could explain that to me, it would be kuch appreciated.

1 Like

This question has been answered countless times before.

I would recommend looking for previous posts on the dev-forum about your question before asking here.

1 Like

I don’t think you understood my question, I understand the functionality of self. However, I do not know what I should make as self.

On mobile:

Lets say I have:

local table = {}
table.__index = table

function table.new()

local self = setmetatable({}, table)

self.? = ?

return self

end

1 Like

I don’t think you actually understand what self means.
As self is implicitly defined as the object table when creating a method. ( Please re-read and understand the first link I sent you to fully comprehend this ).

And a quick note about your code above. In the case above all your doing is creating a variable called self which holds the return value of setmetatable(). Its basically the same thing as doing something like

local if = 5
print(if)

Obviously in the case above if is a variable holding the value 5, and does NOT function the same as it would if you were to use in the syntax of an if statement.

TLDR: Your just creating a variable with a keyword. Self is implicitly defined when creating a method using the table:Method() syntax. And you need to re-read the posts on self in order to understand what it actually means.

1 Like

I was wondering what I should set as self. In other words, I know what self means, I know how to use it, but I do not know what to make it.

Technically, you can set it to whatever you want since self is not a reserved keyword but just an identifier. In your specific example (changed some variable names because you were shadowing the built-in table library):

local MyClass = {}
MyClass.__index = MyClass

function MyClass.new()
    local self = setmetatable({}, MyClass)
    self.MagicValue = math.pi
    return self
end

local obj = MyClass.new()
print(obj.MagicValue) -- 3.1415...

Though in this case you could just create the table with the MagicValue field pre-populated:

local self = setmetatable({ MagicValue = math.pi }, MyClass)
return self

What you call the variable does not matter, self is not “special” here like it is with the method declaration syntax

1 Like

When using self inside of functions in a table such as

function module:foo()
    self.something
end

self is just a place holder for the table that called the function. you can do the same thing with different syntax by doing this:

function module.foo(tbl)
    tbl.something
end

let’s test it by doing this:

-- in a module script:
local module = {}
module.value = 10

function module:foo()
    print(self.value)
end

return module

you would call this from a script like so(assuming that you are requiring a module script):

local module = require(module_path)
-- we can now call that same thing two different ways
-- way one:
module:foo() -- prints the value of 10

-- we can also do this:
module.foo(module) -- this does the same thing

you can also try changing the value from the server script and see that it prints the new value.

1 Like

So would this in a general sense be what you mean? (just the module)

local Inventory = {}
Inventory.__index = Inventory

function Inventory.New(Player)
	local self = setmetatable({}, Inventory)

	self.Backpack = Player.Backpack

	return self

end

function Inventory:AddItem(Item)
	Item.Parent = self.Backpack
end	

return Inventory
1 Like

@Xx1Luffy1xX @dragonvale5555 @jakedies

Thank you guys for the posts, I believe that I understand what you guys are saying.

2 Likes

self in OOP references your module, that’s all. It is the same doing Base.Health as self.Health. However self is preferred over the initial method.

1 Like