When should I use :

I have never understood how to use : , for example I have seen it in functions like these:

 function Util:Round(number, factor) -- This line in "Util:Round"
	local mult = 10 ^ (factor or 0)
	return math.floor((number * mult) + 0.5) / mult

Would you help me to understand it?

1 Like

There’s no difference at all between using “.” and “:” in practical terms, both will execute the function.

Some people prefer different kinds of appliances, but at the end of the day you’re the one to decide if you want to use “.” or “:”.

I personally like to use both, but I want to choose which one I’ll use for the entire module.

Edit: I’m quite sure one of them passes “self” as one of the arguments to the function, but I forgot which one it was, if you’ll never use self then it’s not necessary.

5 Likes

“.” calls it like a function, and “:” calls it like a method, i.e., implicitly passing the self argument, which is equal to whatever table it was called on.

local counter = {count = 0}
function counter:Increment()
    self.count += 1 --self and counter are the same thing in this case
end
counter:Increment()
print(counter.count) --> 1
3 Likes

Thanks, I never knew self meant the table it was called on.