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.
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
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!