for example
1 | function Noob:Print() |
2 |
3 |
4 |
5 | or function Poop.Fart |
The main difference here is that :
signifies a method while .
signifies that a function/value is a member of a table. Also, with any function declared using :, the self parameter is essentially set to the table the function is contained within, unlike functions declared using :, functions declared using "." doesn't have a set self parameter, you can try this yourself by doing the following:
1 | local tbl = { } |
2 |
3 | function tbl:thing () |
4 | print (self) --prints the table "tbl" |
5 | end |
6 |
7 | functib tbl.thing 2 () |
8 | print (self) --prints nil |
9 | end |
Methods created by the : syntax are extremely useful with Object oriented programming and class constructors. Such a purpose is demonstrated in the 16th chapter of the lua pil
The main difference is that ':' has self
as it's first argument without actually typing it down.
01 | Humanoid = { health = 100 } |
02 |
03 | function Humanoid:TakeDamage(dmg) |
04 | self.health = self.health - dmg -- i didn't need to put self as an argument but I can still use it |
05 | end |
06 |
07 | -- equivalent |
08 | function Humanoid.TakeDamage(self, dmg) |
09 | self.health = self.health - dmg |
10 | end |
11 |
12 | -- usage |
13 | Humanoid:TakeDamage( 10 ) -- you usually see this |
14 | Humanoid.TakeDamage(Humanoid, 10 ) -- the same |