------|Forewords|------
Welcome to the second guide that I release.
If you feel that you understand the content in my first guide
http://www.mmowned.com/forums/emulator-server-guides/220201-lua-scripting-beginners-1-0-a.html
Then this is the place for you!
In the last guide we learned how to script the bascics.
In this guide on the otherhand it will get a little bit more advanced.
We're going to script a NPC so that he will perform certain actions at a certain percentage!
Let's start!
As in any script we're going to start with the "function" line.
Code:
function NPC_Function/Phase(Unit, Event)
Then we're adding some action!
Let's suppose we want the boss to cast a spell and yell at 95%
Code:
function NPC_Function/Phase(Unit, Event)
if Unit:GetHealthPct() < 95 then
Unit:SendChatMessage(14, 0, "Message")
Unit:FullCastSpellOnTarget(43488, Unit:GetRandomPlayer(0))
end
end
Alright lets review.
if Unit:GetHealthPct() < 95 then
This signals the NPC to do a certail action at the certail percentage.
Unit:FullCastSpellOnTarget(43488, Unit:GetRandomPlayer(0))
FullCastSpellOnTarget - Cast's a (often) Offensive spell with casting time.
43488 - This is the spell ID I chose, this is the spell "Wrath of the Lich King"
It one shots a player.
Unit:GetRandomPlayer(0) - Makes the NPC target a random player and cast the spell.
Let's review the script as far as we are now.
Note: Look out for the new stuff now that you've never seen. And I added a yell when the NPC enters combat.
Code:
function Arthas_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "Fools, all of you!")
Unit:RegisterEvent("Arthas_PhaseOne", 1000, 0)
end
function Arthas_PhaseOne(Unit, Event)
if Unit:GetHealthPct() < 95 then
Unit:RemoveEvents()
Unit:SendChatMessage(14, 0, "Now you die!")
Unit:FullCastSpellOnTarget(43488, Unit:GetRandomPlayer(0))
end
end
"Unit:RegisterEvent("Arthas_PhaseOne", 1000, 0)"
Okay, so what the heck is this?
This is a command similiar to the "RegisterUnitEvent" But this is often used to register spells and/or events that occurs in the middle of a fight.
1000 - This is how many seconds after 95% The script will activate. (1000 = 1 sec etc etc)
0 - This signals how many times it should occur.
That's pretty much it, you'll soon get a hang of it.
More guides coming up :-)
Next guide will contain a druid world boss!
Don't miss that one!
*Prowls into the shadows*
Thank you for this time, I hope you learned anything from this guide.
Farewell!
Code:
function Arthas_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "Fools, all of you!")
Unit:RegisterEvent("Arthas_PhaseOne", 1000, 0)
end
function Arthas_PhaseOne(Unit, Event)
if Unit:GetHealthPct() < 95 then
Unit:RemoveEvents()
Unit:SendChatMessage(14, 0, "Now you die!")
Unit:FullCastSpellOnTarget(43488, Unit:GetRandomPlayer(0))
end
end
RegisterUnitEvent(NPCID, 1, "Arthas_OnCombat")
Thanks to
knaur - For teaching me so much!
NE Team - For additional help.