Difference between : and

So I saw some modules on the forum using functions. And some used module:function() and others do module.function()

Why does using semi colons return a table and a period acts like a normal function?

And what would be a realistic use of using the semi colons

semi colons = implicit self declaration in the args

: just carries self when you’re using tables.
. doesn’t carry self when using tables.

--if you want to access something in the main table, use :
function Main:DoThis()
   local a = self.a --you get self
end

--If you're making a function and you don't want self just use .
function Main.DoThing()
   --etc
end

--If you DO want self, you would have to place it as one of the arguments
function Main.DoThing(self)
   --etc
end

it should also be noted if you know the object you are getting the function to you could also do smth like this

game.GetService(game, "RunService").Heartbeat:Wait()
1 Like

Others have already covered it, but maybe this helps too.

It’s basically just short-hand code. Some call it “syntactic sugar.”

When you call a table function with :, it injects the table itself as the first argument:

myTable.Hello(myTable, "Test")

SAME AS:

myTable:Hello("Test")

When you define a table function with :, it creates a hidden self parameter as the first parameter.

function myTable.Hello(self, msg) print(self == myTable) end

SAME AS:

function myTable:Hello(msg) print(self == myTable) end

So ideally, you match the caller and the definition. If it’s defined using :, then it makes sense to call it using :. If it’s defined using ., then call it with . too.

4 Likes