How can I use SELF?

I want to know how to use self like:

self.thing()
1 Like

As @itsXtimes said check out the link. There are some sample of

1 Like

For example in modules, where you invoke functions ( call with : ), then the first argument passed is automatically the module which has the function you’re invoking. That first argument can be used as “self” in the function arguments.
Sorry if I didn’t explain this clearly, tell me what you do not understand and I’ll try to say it better.

2 Likes

Ok, thanks! :grinning: :grinning: :grinning:

1 Like

Here’s a code sample:

module = {}
module.value = 5
module.increase = function(self,value)
self.value = self.value + value
end
print(module.value) -- 5
module:increase(3)
print(module.value) -- 8

You don’t actually have to name the argument as self and any other name would be fine, but “self” is a good way to identify it.

2 Likes

I don’t get it…
so “self” is the function name?

because you said

then the first argument passed is automatically the module

1 Like

Needs to be

module:increase(3)
-- or
module.increase(module, 3)

Otherwise self won’t get passed

2 Likes

self in OOP is the table that contains methods or the table you are referencing

1 Like

Apologies, I didn’t notice that

1 Like

One more thing, if you define the function using syntax like this

function module:increase(value)
self.value = self.value + value
end

Then the name would remain self and would be unchangeable.

1 Like
local Sword = {}

Sword.__index = Sword

function Sword.new()

return setmetatable({}, Sword)

end

function Sword:TakeDamage()

--//self is basically sword The table that you are referencing when creating functions

end
2 Likes

If you wanted to use self, you could do:
module.increase(module,3) and use self as the first argument name inside the function (which is the module) afterwards
But there is syntactic sugar which is much easier to write and much prettier which is replacing . with : and removing the first argument which is the module since it would be included automatically:
module:increase(3)

1 Like
local Sword = {}

Sword.__index = Sword

function Sword.new() --//Creating the basic framework of the sword
   
   self.Damage = 50 --// == Sword.Health = 0
   self.Attacking = false --// == Sword.Attacking = false
   
   return setmetatable({}, Sword)
end

function Sword:TakeDamage()
   self.Attacking = true --//Changing the value in sword to true
end
1 Like

oh i get it now, thanks :slight_smile:

Stupid example you can use for minigames

local Class = {}

function Class.new()
	Class.Money = 0
end

function Class:PlayerWin()
	self.Money = self.Money + 10
	print("You have won the minigame")
end

Class.new()

wait(5)

print(Class.Money) --Before they won

Class:PlayerWin()

--//After they won

print(Class.Money)

If this was a minigame you would set there MoneyValue In class equal to their actual leaderstats Value but this is an example
2 Likes

Will this call the argument of the function? if I use self outside of a function can I call any argument or variable? I’m not entirely clear, does it also work with normal script?

Self use to returns the table the function is being called from.

local hi = {} 
function hi.__tostring(self, ...)
    return "hi table!"
end

function hi:something(...)
    print(tostring(self))
end

Try that and u will know how it works

Extra Information:
https://www.lua.org/pil/16.html

2 Likes

self is used in OOP (Object Oriented Programming) in metatables.

local module = {}
module.__index = module
function module.new()
    local self = setmetatable({},module)
    return self
end

Now every time you make a function in the module it is added to the metatable that has been declared as self.

1 Like

Not only for OOP only. Self can also use for function, table, etc.

1 Like