It has already been answered but a bit more explanation.
module:function()
is syntax sugar for module.function(module)
. So when you call a module using :
you’re actually typing module.function(module)
.
Now when the function itself in the module is written using :
it unlocks a new keyword names self
.
This self
keyword is a table to the module. For example:
local module = {}
function module:foo()
print(self) --> returns this module with the functions foo and bar
end
function module.bar()
print(self) --> returns nil
end
module:foo()
module:bar()
Now remember how calling functions using :
was the same as giving the module as the first parameter? This is only the case if the function in the module was written with a dot. For example:
local module = {}
function module:foo(param1, param2)
print(self, param1, param2)
end
function module.bar(param1, param2)
print(param1, param2)
end
module:foo(1) --> prints: the module, 1, nil (since the : with : doesn't provide the module)
module.foo(1) --> prints: 1, nil, nil (self is now 1 not param1, since the : assumes the first argument is the module itself, and calling with . doesn't provide the module)
module:bar(1) --> prints: this module, 1
module.bar(1) --> prints: 1, nil (since it doesn't provide the module as the first argument)
Now to be safe, just make sure you call the function with the same character as you made it with and the parameters won’t change.
IN SHORT.
If you made the function with :
, it expects the first parameter to be the module itself. self
will become the first parameter given to the function.
If you made the function with .
, it will act like a normal function, self
will be nil.
Calling functions with :
will automatically make the first parameter the module itself and shifts all other parameters (1 will become 2 etc).
Calling functions with .
will not modify any parameters.
Here’s a quick lookup table.
made with :, called using : | self will be the module, param1 will be param1
made with :, called using . | self will be param1, param1 will be param2 etc
made with ., called using : | self will be nil, param1 will be the module, param2 will be param1 etc
made with ., called using . | self will be nil, param1 will be param1