This guide is for understanding what goes on in a script that was made for World of Warcraft.
You may not understand everything, but I'll explain everything you need to know to the best of my abilities.
::Tools you'll need/want::
Notepad++ -- download this if you don't have it yet
Lua.org -- Official Lua manual (but not for WoW)
Lua Values -- refer to this thread often.
Older Command List -- best one I could find, please note, it is out-of-date, but it does have usable commands.
Command List -- the official Command List of ArcEmu
There is no perfect way to learn scripting, trial and error is how most of us learned. All of us would agree that it's much more fun to have it work the first time around.
::Basic Structure of a Function::
Our goal in this example is to simply understand the structure of Lua. Here's a very basic example of a proper function.
function Name_Of_Function(arg1, arg2) The Body of the Function end
function
-- this is simply telling the script, "Hey dude, I'm making a new function."
Name_Of_Function
-- this, as you can see, is the name of the function you want to create.
(arg1, arg2)
-- in the parenthesis are the function's arguments. these arguments are very important. they contain data for the LuaEngine to read.
The Body of the Function
-- this is where you put all your commands you want to do. whenever the function is used, those commands will be used too.
end -- this fella tells the LuaEngine, "Hey, the function only goes on to this point." All lot of scripts you'll see and create will use conditional statements. "if .... then" ...yeah. when you see an "if" in lua, you're telling the LuaEngine that you want to do something under that condition. you'll need to add another "end" to the function where ever you want the condition to stop being read.
Conditional Statements
I'll explain with an example, using a working function.
function OnKillPlayer(event, killer, victim) if victim:GetLevel() >= 80 then killer:AddItem(12345, 5) end end RegisterServerHook(2, "OnKillPlayer")
Here's what the above example does:
1. The function is triggered whenever a player is killed. (SERVER_HOOK_ID is 2)
2. If the victim player was level 80, give the killer player 5 copies of the item with the EntryID of 12345.
Now, lets break it down into small bits. This is explaining conditional statements.
function OnKillPlayer(event, killer, victim) if victim:GetLevel() >= 80 then killer:AddItem(12345, 5) end end RegisterServerHook(2, "OnKillPlayer")
Lets take a look a the arguments. You can see that we have 3 of them: 'event', 'killer', 'victim'. I made it so that it was easy to see what the LuaEngine is reading.
Lets look at our conditional statement now...
if victim:GetLevel() >= 80 thenif -- tells the LuaEngine that we only want something to happen under a condition.
victim -- 'victim' is the argument in our function, the LuaEngine understands that we want this argument to do the following command.
:GetLevel() -- this is a command. it gathers information about the argument using the command (in this case, we are gathering data about the argument 'victim')
>= -- this is a relational operator. you may have seen with while in math class http://www.lua.org/m...nual.html#2.5.2
80 -- this is a parameter. in this case, we are looking for the victim's level. we simply told the LuaEngine that we only want it to compare against '80'.
then -- tells the LuaEngine that the condition is made and, if the conditional we made checks out okay, to do the following...
if victim:GetLevel() >= 80 then -- if the victim's level is greater than, or equal to, 80 then do this...
Reposting our little script...
function OnKillPlayer(event, killer, victim) if victim:GetLevel() >= 80 then killer:AddItem(12345, 5) end end RegisterServerHook(2, "OnKillPlayer")
Lets look at what happens when our conditional statement is met...
killer:AddItem(12345, 5)killer -- this is another argument in our function, this holds the data for the player who kills the victim.
:AddItem(arg1, arg2) -- so, this obviously gives the argument an item. how does it work?
:AddItem(arg1, arg2)arg1 -- this is the EntryID of the item you want you give the player
arg2 -- this is the amount of that item you want to give the player.
In our case, we are giving "killer" 5 copies of the item with the EntryID of 12345.
So, here's what you know:
"if the victim was level 80 or higher then give the killer 5 copies of item: 12345"
Well, know we need to close our function so it can be read correctly. You'll notice how we have two "end" in the script. Conditional statements need to be closed too. You need to tell the LuaEngine that you are finished giving the commands to the condition.
At the same time, if you are done with your function, you need to close it off with an "end" too.
function OnKillPlayer(event, killer, victim) if victim:GetLevel() >= 80 then killer:AddItem(12345, 5) end -- this closes the conditional statement... the "if .... then" thing end --this closes the function RegisterServerHook(2, "OnKillPlayer")RegisterServerHook(2, "OnKillPlayer") -- this will do the function specified when triggered. in this case, it will do the function "OnKillPlayer" when triggered by a player killing a player (ID = 2).
RegisterServerHook -- these are triggered events. know that there are different types of these events for different uses.
::Try it yourself::
Now, lets make a very simple function. How about this...:
Open Notepad++
When you have it opened, go to "Language" -> "L" -> Lua ;; and select it.
...we are just preparing you for this next example...
function OnSpawn(Unit, event) Unit:SendChatMessage(14, 0, "Hello world!") end
quick rundown, make the Unit yell (14), "Hello world!" in a universal language (0).
Unit -- this is just another argument, for now, assume that this function pertains to an NPC.
"Hello world!" -- you may notice how there are quotation marks around the message. this is known as a string. "Hello" -> Hello (when viewed in-game)
In your Notepad++ program, make the Unit yell, "Yelling makes red letters!" in a universal language. Don't worry about the function line, or the arguments on the top yet.
the outcome should look exactly like this:
Unit:SendChatMessage(14, 0, "Yelling makes red letters!")that's really it.
Just making these 1 line practice scripts helps you understand what you're actually doing. Play around with a few commands and learn how to use them. Questions are expected, we've all been there; so don't hesitate to ask around.

















