What does 'self' mean?

Put simply, what does

self

mean?
I see it in module scripts, and i don’t understand yet. For example, in Datastore2 there’s this:

function Datastore2:Debug(...)
    if self.debug then
        print(...)
    end
end

It’s probably simple, but I don’t understand right now.

1 Like

self refers to the same table when it is called the way you did, if it is with dot it does not work Will give nil. That’s all, more information: Functions | Roblox Creator Documentation

1 Like

Actually, it does work with dot but you would have to manually pass the self variable.

game:GetService('ReplicatedStorage')
-- is the same as
game.GetService(game, 'ReplicatedStorage')

And when defining the function,

function Datastore2:Debug(...)
-- is the same as
function Datastore2.Debug(self, ...)
4 Likes

Self refers to a table inside a module script or normal script, here is a example:

local module = {}

self:Collect() -- this means module:Collect()

I’m sure this is what self means.

You can’t define a function/method like that.

You would have to do

local module = {
    ['Collect'] = function(self)
        print('collect', self)
    end;
}

or

local module = {}

function module.Collect(self)
    print('collect', self)
end

or

function module:Collect()
    print('collect', self)
end

All three of those are the exact same thing.

2 Likes

self is a way to refer to the table you’re performing a method on. Doing something like “Instance.Clone(Instance)” is the same as “Instance:Clone()”. The only difference is that methods that use the colon like :Clone() pass self automatically, while using Instance.Clone(Instance) means you need to pass the Instance itself manually

3 Likes