for example
function Noob:Print()
or function Poop.Fart
for example
function Noob:Print()
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:
local tbl = {}
function tbl:thing ()
print(self)--prints the table "tbl"
end
functib tbl.thing2 ()
print(self)--prints nil
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.
Humanoid = {health = 100}
function Humanoid:TakeDamage(dmg)
self.health = self.health - dmg -- i didn't need to put self as an argument but I can still use it
end
-- equivalent
function Humanoid.TakeDamage(self, dmg)
self.health = self.health - dmg
end
-- usage
Humanoid:TakeDamage(10) -- you usually see this
Humanoid.TakeDamage(Humanoid, 10) -- the same
This discussion is preserved as it was. New answers, comments, and votes are closed.