Difference bettween ModuleName:FunctionName() and ModuleName.FunctionName()

Hi,
I came across a module script which had the following format for declaring a global function

function ModuleName:FunctionName()
    -- code here
end

So, whats is the difference from using the above and this :

function ModuleName.FunctionName()
    -- code here
end

Any help is appreciated

the : notation passes ‘self’ automatically as the first argument, to my knowledge, this is only done when writing with OOP (Object Oriented Programming)
here is more about how it works:

2 Likes

If u use function module:BlahBlahBalh() it passes ‘self’ . you might ask, what is self? Self is basically the table that the function is stored in .

eg)

local module = {}
module.Player = "skyblox"

function module:SkybloxBest()
print(self)
end

function module:Test()
print(self)
end

return module

if i use the function SkybloxBest, it will print the variable called module which is a table that contains Player, function SkybloxBest() and function Test()

however , if you use function module.Hi() , the variable self will be nil

2 Likes

Oh ok thanks! This will help as im planninng to switch to OOP.

1 Like

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