Functions

Working on a simple command system today, I was thinking about the different ways functions im Gmod work. I mean,

[lua]
ply:Kill()
hook.Add()
fun()
[/lua]

I’m just giving examples. I mean, I pretty much know how the second and third ones work, second being part of a library, hook in the above example ( yes I know it takes arguments ) and the third one is a basic function. What I’m unsure of is the first one though. While I know it is uses for hooks like GM:PlayerDeath() and such, I am not sure what ply:Kill() means. Yes, I know it kills the player. But why not just do Kill( ply ) instead? Does someone have a link to a decent tutorial or can easily describe it? Thanks

Kill is a method that a player reference can call.
Kill is not a global function, and does not take any arguments.

ply is just a common variable name that can be thought of as a handle or pointer to a player.
it’s like a zipcode or phone number to uniquely ID a specific player.

So you first have to have their phone number, then you can tell them to kill themselves.
You can’t just pick up the phone and say Kill yourself and then say their phone number to the dial tone.

And you shouldn’t do that anyway, I think that would be illegal so don’t make calls like that, this is just an example so you can understand. I’m not endorsing or suggesting you do any illegal activities.

ply:Kill() is similar to GM:PlayerDeath() in some ways. ply references a metatable, which contains the function meta.Kill, while GM is a table with the function table.PlayerDeath

When you use : to call a function, you’re basically creating an invisible argument: self.
So, when you use ply:Kill(), self is ply in the function.