Module Scripts: What is the difference between : and .?

I have been using module scripts for a while was have been seeing many module examples with functions as:

local DataManager = {}

function DataManager:AddItem(Player, ItemID, Quantity)
--Code here
end

return DataManager

And others as:

local DataManager = {}

function DataManager.AddItem(Player, ItemID, Quantity)
--Code here
end

return DataManager

You can see that the only difference is the table:function and table.function, is there any difference between these two?
some of the functions are supposed to return variables while others aren’t.

1 Like

Question has been asked before, see below :slight_smile:

2 Likes

In addition, : and . aren’t module script only.

An additional question as well, can I do

local DataManager = {}

function DataManager:Inventory:AddItem(Player, ItemID, Quantity)
--Code here
end

return DataManager
end

Colon syntax is useful for OOP because it passes the table that the function is a member of as self. You can see that if you define and call a function with a colon operator, like below, you will have a variable self.

local t = {}
function t:something()
     print(self == t)
end
t:something()
t.something(t) -- equivalent to the above; colon operator is just syntactic sugar
1 Like

That would not work. It’s not closing off anything. The scope delimiters in Lua are end, but there is nothing to close.

Edit:
And I missed the other : and that won’t work

Colon operator is only used when calling a function. If you’re not calling the function immediately after you index it with the colon operator, it will error.

Oh okay, thanks! I’m working on the data system of my RPG so I wanted to make sure of the difference in order to have as little bugs as possible and being as efficient as possible.
Thanks @posatta and @incapaxx!

1 Like