Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Difference between using a colon or a dot in modules?

I've just started to fiddle around with modules, and I'm wondering whether there's a difference between doing

function module:foo()
	print("Hello World!")
end

and

function module.foo()
	print("Hello World!")
end

They both seem to do the same thing when called from a server/local script. Is there a difference, or is it just preference? And if there is a difference, when do you use which?

2 answers

✓ Accepted answer

Basically, calling a method by using : causes lua to pass self as the first argument.

Self is the object you're invoking the method on, so if you do let's say game:Destroy(), lua passes game as self.

Consider this example:

local obj = {}
obj.hi = function(foo, bar)
	print(foo)
	warn(bar)
end

obj.hi("hello", "world") --> hello	world
obj:hi("hello", "world") --> table: 0x12345	hello

As you can see, the obj table is passed as self, that is the first argument of the function.

1 comment

0

Thanks.

As Expodo said, using the method syntax will automatically define self as the table the function is in.

function foo:bar() print(self) end is syntactic sugar for function foo.bar(self) print(self) end

1 comment

0

So when you want to use self you use the colon, otherwise use the dot?

This discussion is preserved as it was. New answers, comments, and votes are closed.