This guide will show you how to do various Things in lua
----------
1 Scripting NPC's
- Say something
- Cast spells
- Summon other mobs
- Make phases (Change its scale, displayid Etc..)
2. Script GOs
3 Teleporter NPC
----------
If well accepted i will keep this updated!
And off we go.
A script in lua does something that you cant do with just in game commands.
It is what make boss encounters more then just you walk in and kill them.
Here is what a function looks as a blank slate - With NO data entered into it.
Code:
function Nameofthefunction(Unit, Event)
end
Lets say my NPC is called Demon.
Code:
function Demon_OnCombat(Unit, Event)
end
The name of my NPC is now in the script.
Note: Nameofthefunction or Demon_Oncombat actually can be anything. It really helps to keep it organized if you do it this way.
NEXT PART!
To make the NPC say something when engaged heres what you do..
Code:
Unit:SendChatMessage(12, 0, "Heres where the text goes.")
The 12 in that script. Right at the beginning of the parenthesis, Is how you want the NPC to talk....
Be it...
Code:
12 – npc say
13 – npc whisper
14 - npc yell
Universal means anyone can here it.
Heres some other languages. Probably wont be used as much.
Code:
0 = Universal
1 = Orcish
2 = Darnassian
3 = Taurahe
6 = Dwarven
7 = Common
8 = Demonic
9 = Titans
13 = Gnomish
14 = Zalandari
33 = Gutterspeak
35 = Draenei
Now we add the Chat on engage to the OnCombat Script
Code:
function Demon_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "Text goes Here!")
end
And again. The "SendChatMessage" does not have to be those exact words. It just helps to keep things organized for yourself or others reading the script.
To get the script to be able to function you have to Register it.
Registering the Function has to be at least one line below the function itself.
For each function you need to have a Different Register command.
The Register commands do not need a line between them unlike the functions
Code:
RegisterUnitEvent(NPC-IDHERE, 1, "Function name")
The Function name for example from our earlier script is "Demon_OnCombat
After the ID there is a 1 if you notice. that defines what the "unit" event in the function. This is what makes the functions different.
Here is a list:
Code:
1 = Enter combat
2 = Leave combat
3 = Killed target
4 = When Npc is dead
6 = Spawn
7 = Gossip talk
8 = Reach waypoint
10 = When player enters range
This is what a Function would look like for
Demon - ID 9995
Script - Activates on combat.
Details - On combat "Demon says "Darkness Reigns!"
Code:
function Demon_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "Darkness Reigns!")
end
RegisterUnitEvent(9995, 1, "Demon_OnCombat")
here are some different Examples of different Function events.
Code:
function Demon_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end
function Demon_OnKilledTarget(Unit, Event)
end
function Demon_OnDied(Unit, Event)
Unit:RemoveEvents()
end
They speak for themselves in what they do.
The RemoveEvents are for if the scripts make the NPC change size or Change displayid it will reset it.
To register the last 3 scripts do the next code.
Code:
RegisterUnitEvent(NPC-IDHERE, 2, "_OnLeaveCombat")
RegisterUnitEvent(NPC-IDHERE, 3, "_OnKilledTarget")
RegisterUnitEvent(NPC-IDHERE, 4, "_OnDied")
Putting it all together with every script we have done so far would look like the following.
Code:
function Demon_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "I Will Kill You!")
end
function Demon_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end
function Demon_OnKilledTarget(Unit, Event)
end
function Demon_OnDied(Unit, Event)
Unit:RemoveEvents()
end
RegisterUnitEvent(9995, 1, "Demon_OnCombat")
RegisterUnitEvent(9995, 2, "Demon_OnLeaveCombat")
RegisterUnitEvent(9995, 3, "Demon_OnKilledTarget")
RegisterUnitEvent(9995, 4, "Demon_OnDied")
Here is where it gets more in depth by adding spells.
Spells require a different function obviously.
Code:
function Nameofthefunction(Unit, Event)
end
Back to stage 1. make a Brand New Function like above.
Code:
function Demon_Spell1(Unit, Event)
end
Code:
Unit:FullCastSpellOnTarget(spellid, Unit:Target)
The Target in "Unit:Target" Can be one of the following.
Code:
- GetRandomPlayer(0) = Will make the mob target a random player
- GetRandomPlayer(1) = Target random player in short range
- GetRandomPlayer(2) = Target random player in medium range
- GetRandomPlayer(3) = Target random player in long range
- GetRandomPlayer(4) = Target random player with mana
- GetRandomPlayer(5) = Target random player with rage
- GetRandomPlayer(6) = Target random player with energy
- GetRandomPlayer(7) = Target random player, main tank won't be targeted
- GetMainTank() = targets Main tank.
- GetClosestPlayer() = Targets whoever is closest.
If - for example the boss was going to attack the closest person
Code:
function Demon_Spell1(Unit, Event)
Unit:FullCastSpellOnTarget(5, Unit:GetClosestPlayer())
end
In detail - In a certain amount of milliseconds and how many times in that amount of times it is casted.
Code:
Unit:RegisterEvent("functionname", MIlliseconds, howmanyoftimesinthat time)
Heres what it would look like to let the NPC cast the spell 2 times in a total of 60 seconds
Code:
Unit:RegisterEvent("Demon_Spell1", 60000, 2)
To make it activate OnCombat (as in when engaged - be it Aggroed or attacked deliberately.)
Code:
function Demon_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "Darkness Reigns")
function Demon_Spell1(Unit, Event)
Unit:FullCastSpellOnTarget(5, Unit:GetClosestPlayer())
end
function Demon_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end
function Demon_OnKilledTarget(Unit, Event)
end
function Demon_OnDied(Unit, Event)
Unit:RemoveEvents()
end
RegisterUnitEvent(9995, 1, "Demon_OnCombat")
RegisterUnitEvent(9995, 2, "Demon_OnLeaveCombat")
RegisterUnitEvent(9995, 3, "Demon_OnKilledTarget")
RegisterUnitEvent(9995, 4, "Demon_OnDied")
RegisterUnitEvent("Demon_Spell1", 60000, 2)
end
BOSS PHASES!
Boss phase time!
Code:
function FunctionName(Unit, Event)
if Unit:GetHealthPct() <= X then
end
end
X is where the NPC will check his Health and act according to what we will script.
Gonna use 35%
Now the X is replaced with 35
Code:
function Demon_Phase1(Unit, Event)
if Unit:GetHealthPct() <= 35 then
end
end
At 35% we want the Boss to cast a spell. so we do....
Code:
Unit:FullCastSpellOnTarget(spellID, Unit:target)
FullCastSpellOnTarget is a command when its a spell with a cast time (not instant =/)
For an Instant Cast use CastSpell command / like so.
Code:
function Demon_Phase1(Unit, Event)
if Unit:GetHealthPct() <= 35 then
Unit:CastSpell(27082)
end
end
Also You can make him send a message before or after casting the spell.
Code:
Unit:SendChatMessage(14, 0, "Write whatever you want.")
And now the function should look like this:
Code:
function Demon_Phase1(Unit, Event)
if Unit:GetHealthPct() <= 35 then
Unit:SendChatMessage(14, 0, "Casting Spell!")
Unit:CastSpell(27082)
end
end
Also - you can make the NPC change Displayid or their scale
Change its scale:
Code:
Unit:SetScale(scalenumber)
Modify its displayid:
Code:
Unit:SetModel(displayID)
Lets add it to the previous script.
Code:
function Demon_Phase1(Unit, Event)
if Unit:GetHealthPct() <= 35 then
Unit:SetModel(145)
Unit:SetScale(2)
Unit:SendChatMessage(14, 0, "Casting Spell!")
Unit:CastSpell(27082)
end
end
The RegisterEvent Looks like this.
Unit:RegisterEvent("Demon_Phase1", timeinmilliseconds, howmanytimes)
adding it to the script!
Code:
function Demon_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "Casting Spell!")
Unit:RegisterEvent("Demon_Spell1", 60000, 2)
Unit:RegisterEvent("Demon_Summon", 60000, 2)
Unit:RegisterEvent("Demon_Summon2", 60000, 2)
Unit:RegisterEvent("Demon_Phase1", 5000, 1)
end
function Demon_Phase1(Unit, Event)
if Unit:GetHealthPct() <= 50 then
Unit:SetModel(145)
Unit:SetScale(2)
Unit:SendChatMessage(14, 0, "I shall kill you!")
Unit:CastSpell(27082)
end
end
function Demon_Summon2(Unit, Event)
Unit:SpawnCreature (9994, -8919.73, 47.95, 94.14, 0.48, 14, 60000);
end
function Demon_Summon(Unit, Event)
local x = Unit:GetX();
local y = Unit:GetY();
local z = Unit:GetZ();
local o = Unit:GetO();
Unit:SpawnCreature (9994, x, y, z, o, 14 ,60000);
end
function Demon_Spell1(Unit, Event)
Unit:FullCastSpellOnTarget(5, Unit:GetClosestPlayer())
end
function Demon_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end
function Demon_OnKilledTarget(Unit, Event)
end
function Demon_OnDied(Unit, Event)
Unit:RemoveEvents()
end
RegisterUnitEvent(9995, 1, "Demon_OnCombat")
RegisterUnitEvent(9995, 2, "Demon_OnLeaveCombat")
RegisterUnitEvent(9995, 3, "Demon_OnKilledTarget")
RegisterUnitEvent(9995, 4, "Demon_OnDied")
[/code]
BTW
For the Pink Text - you can use one of these
Code:
- GetRandomPlayer(0) = Will make the mob target a random player
- GetRandomPlayer(1) = Target random player in short range
- GetRandomPlayer(2) = Target random player in medium range
- GetRandomPlayer(3) = Target random player in long range
- GetRandomPlayer(4) = Target random player with mana
- GetRandomPlayer(5) = Target random player with rage
- GetRandomPlayer(6) = Target random player with energy
- GetRandomPlayer(7) = Target random player, main tank won't be targeted
- GetMainTank() = targets Main tank.
- GetClosestPlayer() = Targets whoever is closest.
PORTAL SCRIPTS
Code:
function ScriptName(pUnit, Event, pMisc)
pMisc:Teleport(MapID, X-Coord, Y-Coord, Z-Coord)
end
RegisterGameObjectEvent(GameObjectsENTRYID, 2, "ScriptName")
Look at the first line one more time.
Code:
function ScriptName(pUnit, Event, pMisc)
Name it whatever you want - as previously said.
For example.
Code:
function TELEPORT(pUnit, Event, pMisc)
The Next line has more in it. So....
Code:
pMisc:Teleport(MapID, X-Coord, Y-Coord, Z-Coord)
end
To find the X-Coord, Y-Coord, and the Z-Coord type .gps in game.
Again you need to register it.
Code:
RegisterGameObjectEvent(GO ID, 2, "ScriptName")
What ever GO id you use for the portal should go here
Needs to be what you named the script earlier.
Heres an example of a completed Portal Script.
Code:
function MALLTele(pUnit, Event, pMisc)
pMisc:Teleport(530, -2009.403320, 6590.512695, 12.360116)
end
RegisterGameObjectEvent(999998, 2, "MALLTele")
Teleporter NPC - How-To
Start with-
Code:
function On_Gossip(unit, event, player)
Is the beginning of the Function.
Code:
unit:GossipCreateMenu(High#, player, 0)
Now the chat options need to be defined.
Code:
unit:GossipMenuAddItem(0,"Horde Cities", 0, 0)
After clicking Horde Cities it heads into a Submenu.
Code:
unit:GossipSendMenu(player)
end
Code:
function On_Gossip(unit, event, player)
unit:GossipCreateMenu(100, player, 0)
unit:GossipMenuAddItem(0,"Horde Cities", 0, 0)
unit:GossipSendMenu(player)
end
Onto the Sub-Menus.
Code:
function Gossip_Submenus(unit, event, player, id, intid, code)
And now we will edit the Horde Cities, so that it will port us to Ex: Undercity.
write this:
Code:
if(intid == 0) then
a Zero means that it opens a Sub menu.
Containing.......
Code:
unit:GossipCreateMenu(Different then the Gossipmenu one, player, 0)
Now we will start the actual location selections.
Code:
unit:GossipMenuAddItem(0,"Orgrimmar", 50, 0)
unit:GossipMenuAddItem(0,"Undercity", 51, 0)
unit:GossipMenuAddItem(0,"Thunder Bluff", 52, 0)
unit:GossipMenuAddItem(0,"Silvermoon", 53, 0)
unit:GossipSendMenu(player)
end
Now mine looks like this:
Code:
function On_Gossip(unit, event, player)
unit:GossipCreateMenu(100, player, 0)
unit:GossipMenuAddItem(0,"Alliance Locations", 0, 0)
unit:GossipSendMenu(player)
end
function Gossip_Submenus(unit, event, player, id, intid, code)
if(intid == 0) then
unit:GossipCreateMenu(101, player, 0)
unit:GossipMenuAddItem(0,"Orgrimmar", 50, 0)
unit:GossipMenuAddItem(0,"Undercity", 51, 0)
unit:GossipMenuAddItem(0,"Thunder Bluff", 52, 0)
unit:GossipMenuAddItem(0,"Silvermoon", 53, 0)
unit:GossipSendMenu(player)
end
Now you put in the info for each selection.
Note: You can see that the menu number of Orgrimmar is 50, Undercity is 51, Thunder Bluff is 52, Silvermoon is 53.
Now write this:
Code:
if(intid == 50) then
player:Teleport(1, 9985.907227, 1971.155640, 1326.815674)
end
X
Y
Z
This would go to Orgrimmar
Repeat the above until you have one for each.
To register them..
Code:
RegisterUnitGossipEvent(Your NPC Spawn ID, 1, "On_Gossip")
RegisterUnitGossipEvent(Your NPC Spawn ID, 2, "Gossip_Submenus")
Now it looks like..
Code:
function On_Gossip(unit, event, player)
unit:GossipCreateMenu(100, player, 0)
unit:GossipMenuAddItem(0,"Horde Cities", 0, 0)
unit:GossipSendMenu(player)
end
function Gossip_Submenus(unit, event, player, id, intid, code)
if(intid == 0) then
unit:GossipCreateMenu(101, player, 0)
unit:GossipMenuAddItem(0,"Orgrimmar", 50, 0)
unit:GossipMenuAddItem(0,"Undercity", 51, 0)
unit:GossipMenuAddItem(0,"Thunder Bluff", 52, 0)
unit:GossipMenuAddItem(0,"Silvermoon", 53, 0)
unit:GossipSendMenu(player)
end
if(intid == 50) then
player:Teleport(1, 9985.907227, 1971.155640, 1326.815674)
end
if(intid == 51) then
player:Teleport(0, -5028.265137, -825.976563, 495.301575)
end
if(intid == 52) then
player:Teleport(0, -9100.480469, 406.950745, 92.594185)
end
if(intid == 53) then
player:Teleport(530, -4072.202393, -12014.337891, -1.277277)
end
end
RegisterUnitGossipEvent(5, 1, "On_Gossip")
RegisterUnitGossipEvent(5, 2, "Gossip_Submenus")
PS: THE TELEPORTER SCRIPTS OR ANYTHING WITH A COORDINATE ENTRY DOES NOT WORK RIGHT (IE: Doesnt take you to where it says) Just thought u ought to know.
If well Accepted i will update this will more NPCs and More Scripts.
Thanks!
-Thekal