What does : do in a function?

I have seen these functions with “:” in ModuleScripts:

local part = {}

function part:Initiate()

end

return part

What does the “:” do?

It allows you to use self

ex:

local part = {}
part.mydata = 1

function part:Stuff()
print(self.mydata) -- 1
end

function part.Stuff2()
print(self.mydata) -- nil
end

return part
2 Likes
1 Like

self is used in OOP (Object Oriented Programming) in metatables.

1 Like

If a function has a colon operator and you want to call it with the dot operator, do this:

myObject.myFunction(myObject)

which is equivalent to:

myObject:myFunction()
3 Likes

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