Code:
 
if (intid == 1) then
Well, this is your new best friend, the 'if' statement. It is used to check if a certain condition is met. The thing within the brackets is the bit we are checking. I'm sure 'intid' shall come as a.. annoying, reminder to you. Remember when we added options, and we had to define a unique number? That was an intid. So, if the intid that the player selected is equal to 1 ('Teleport me to the mall.'), then we run this part of the script. Get it?



Code:
player:Teleport(MapID, x, y, z)
This is a statement, similar to the Gossip Ones a few lines up. The :Teleport() statement does what it says on the box. It teleports the Unit in question to the specified MapID, x co-ordinate, y co-ordinate and z co-ordinate. These can be found by going to the designated place you want your mall to be in, and typing '.gps' in game. For sake of example, I shall use the Undeveloped half of Ahn'Qiraj. The Co-ords for this is as follows;

Code:
MapID: 1 
	X: -9101.980469 
	Y: 1612.902832 
	Z: 21
So our statement will read

Code:
player:Teleport(1, -9101.980469, 1612.902832, 21)
So now we know what this does, we'll move onto the next statement; player:GossipComplete(). This one is quite simple and it doesn't need a whole paragraph to explain it. It simply closes the gossip menu. Easy. Right?

Code:
end

Wait, we have two ends? Yes, we do, well done, you can count. The 'if' statement requires an end, since it is a loop, and Lua cannot determine when it has ended on it's own.

Let's see what our function looks like now;



Code:
function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc)
	if (intid == 1) then
		player:Teleport(1, -9101.980469, 1612.902832, 21)
		player:GossipComplete()
	end
end
So, we understand what this all does? Good. Let's move onto the other intid; Remove Resurrection Sickness.

Code:
 
function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc)
if (intid == 1) then
player:Teleport(1, -9101.980469, 1612.902832, 21)
player:GossipComplete()
end
if (intid == 2) then
end
end
As you can see, we've created another if loop. This time, it only works if the intid is 2. So, in this function we want to remove the Resurrection Sickness aura ID. Well.. we don't know that, so it's time to hit our all-time friend, wowhead.com. We search in the spell we want, cut off the 5-digit number at the end of the URL and.. we have the ID: 15007. It's all good and well having the ID, but if we don't know the function to remove it, we're stuck. So let's hit the ArcEmu Wiki, using the link I provided above. We're looking for one that will check if the player has the aura, and then if he does, remove it.. hmm.. :HasAura() and :RemoveAura() look like they'll do the job. Let's try them out.


Code:
function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc)
	if (intid == 1) then
		player:Teleport(1, -9101.980469, 1612.902832, 21)
		player:GossipComplete()
	end
	if (intid == 2) then
		if (player:HasAura(15007) == true) then
			player:SendBroadcastMessage("Resurrection Sickness has been removed. Be careful next time!")
			player:RemoveAura(15007)
			player:GossipComplete()
		else
			player:SendBroadcastMessage("You do not have Resurrection Sickness!")
			player:GossipComplete()
		end
	end
end
Eh what? :SendBroadcastMessage()? else? But you said we only needed :HasAura() and :RemoveAura()! And you haven't even explained what the arguments for them are either!

Well, I thought it would be self explanatory, but there we go. OK, I'll guide you through it.



Code:
:HasAura(SpellID)
:RemoveAura(SpellID)
OK, so they are easy.

Code:
:SendBroadcastMessage("message")
This one sends a message (Like the ones you see when you get an addon error, in the chat box) to the Unit specified.


Code:
else
Aha. This is part of the 'if' loop. The else operator is used to make Lua do things if the conditions are not met. It's basically saying this:

Code:
If the value of intid is 2, and the player has the aura '15007', then do this. If he doesn't have that aura, then do this.
Of course, you still need the end for the 'if' and 'function'.

So, now we have two intids, lets look at our function now, and add the last one.


Code:
function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc)
	if (intid == 1) then
		player:Teleport(1, -9101.980469, 1612.902832, 21)
		player:GossipComplete()
	end
	if (intid == 2) then
		if (player:HasAura(15007) == true) then
			player:SendBroadcastMessage("Resurrection Sickness has been removed. Be careful next time!")
			player:RemoveAura(15007)
			player:GossipComplete()
		else
			player:SendBroadcastMessage("You do not have Resurrection Sickness!")
			player:GossipComplete()
		end
	end
	if (intid == 3) then
		player:GossipComplete()
	end
end
We just add a player:GossipComplete() because the option was to close the window, effectively.

The complete script:


Code:
--[[
	My First Gossip Script!
	Tutorial by Neglected
]]

-- Variables
local NPC_ID = 133713

-- On Triggers
function exampleGossipOnTalk(Unit, Event, player)
	Unit:GossipCreateMenu(100, player, 0)
	Unit:GossipMenuAddItem(0, "Teleport me to the mall.", 1, 0)
	Unit:GossipMenuAddItem(0, "Remove Resurrection Sickness.", 2, 0)
	Unit:GossipMenuAddItem(0, "Never mind.", 3, 0)
	Unit:GossipSendMenu(player)
end

function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc)
	if (intid == 1) then
		player:Teleport(1, -9101.980469, 1612.902832, 21)
		player:GossipComplete()
	end
	if (intid == 2) then
		if (player:HasAura(15007) == true) then
			player:SendBroadcastMessage("Resurrection Sickness has been removed. Be careful next time!")
			player:RemoveAura(15007)
			player:GossipComplete()
		else
			player:SendBroadcastMessage("You do not have Resurrection Sickness!")
			player:GossipComplete()
		end
	end
	if (intid == 3) then
		player:GossipComplete()
	end
end

-- RegisterUnitEvents
RegisterUnitGossipEvent(NPC_ID, 1, "exampleGossipOnTalk")
RegisterUnitGossipEvent(NPC_ID, 2, "exampleGossipOnSelect")

Well done! You've created your first ever functioning script. Or at least, using this tutorial. Save it as TeleporterNPC.lua and place it in your /scripts/ folder. Spawn your mob and restart your server (Or type in #reload if you are using Alvanaar's Reload Chat Command script), and enjoy talking to your first script!

Ifs and Elseifs.

So you know how to use ifs and elses.. but, using the following is quite hard to read and monotonous;



Code:
 
 
if (var == true) then
Weee()
end
if (var == false) then
Awww()
end
if (var == nil) then
WTFITSHOULDNTBENIL()
end
Instead, you can replace this with an elseif, depending on the situation. An elseif basically removes the subsequent ifs and ends so that they are easy to read and are inside one if loop. Sound confusing? Here's a visual example.


Code:
if (var == true) then
	Wee()
elseif (var == false) then
	Awww()
elseif (var == nil) then
	WTFITSHOULDNTBENIL()
end
As you can see, this is much easier to read than the previous ones. Quite a snappy topic, really