The_fool asked me how I learned lua, So I told him the basics. I didn't work very hard on this, but maybe it'l inspire some of you like it did me.
i learned it by just fooling around. its pretty easy.
for example, you open this:
http://gmwiki.garry.tv/index.php/Luait has everything you need.
then you see 2 sections:
Engine Bindingsand
Hooks/EventsHooks and events are used to tell when a function is run. Ex, "
When a balloon is popped" is a hook/event.
Bindings do things like set health.
So click on Engine Bindings and find something that looks cool. For example,
_PlayerSetHealth - Changes a player's health. Syntax: <playerid> <newhealth>
Looks good. Lets go back and find a hook to use
eventNPCKilled - An NPC is killed. Syntax: <killerid> <killed>
That works well too. So now open notepad and enter this:
function eventNPCKilled
CAPS DO MAKE A DIFFERENCE, use correct capitalization
'function' means that the following is an EVENT or FUNCTION
now remember it said this:
eventNPCKilled - An NPC is killed. Syntax:
<killerid> <killed>those mean that when the function is called (ie, an npc dies) it will give you those values. so they must be assigned to variables. Edit what you entered to read this:
function eventNPCKilled(killer,killed)
This means that the killer (<killerid>) will be stored in the variable 'killer' and so on.
Now we can use our binding.
_PlayerSetHealth - Changes a player's health. Syntax: <playerid> <newhealth>
We add this line below the last one, doing the same thing, except this doesnt give you information, it needs information, so we must suply it with the proper info.
_PlayerSetHealth(killer, 150)
This means that the variable stored as 'killer' will equal the player who gets the health. (ie, the player who killed the npc)
150 means that we are setting his health to 150.
Now we add an 'end' to tell the game that our function is now over.
It should look like this:
function eventNPCKilled(killer,killed)
_PlayerSetHealth(killer, 150)
end
save it as a .lua file in gmod9/lua
run the game, lua_openscript it, and kill an npc.