Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the point of putting '.' or ':' in a function?

Asked by 6 years ago

for example

function Noob:Print()



or function Poop.Fart

2 answers

Log in to vote
1
Answered by 6 years ago

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

Ad
Log in to vote
5
Answered by 6 years ago

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

Answer this question