• Log in or sign up for an account here: https://www.sythe.org/login

How to Lua script a npc

Sharp Tooth

Apprentice
Joined
Aug 4, 2008
Sythe Gold
0
Messages
785
Location
You don't need to know
How to Lua script a npc
Hey Sharptooth here, im going to teach you today how to script a npc to send chat messages when you enter combat and even when you get killed by the npc. ENJOY!!


When you script a mob your script should look like this:

Code:
function Nameofthefunction(Unit, Event)
end

Now let's pretend that my NPC is called "Sharptooth" and this script is going to make this npc do something when he enters combat

Then we fill in the name of the function and add _OnCombat (remember capital letters are very IMPORTANT!!!!!!

Code:
function Sharptooth_OnCombat(Unit, Event)
end

we edited the name of the function "Sharptooth"
and edited _OnCombat telling the npc when he enters combat this is what he does:

Next To make the Npc something say when you come in combat with it you use this:

Code:
Unit:SendChatMessage(12, 0, "The message the npc will say")

12 is how the Message is said, 12 is for say, 13 for whisper, and 14 for yell

the 0 is the language which it speaks in 0 means universal so all races understand, some other languages you can use

Code:
0 = Universal
1 = Orcish
2 = Darnassian
3 = Tauron
6 = Dwarfs
7 = Common
8 = Demonic
9 = Titans
13 = Gnomish
14 = Troll language
33 = Gutterspeak
35 = Draenei

Now add the new function we made to the script

Code:
function Sharptooth_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "I Will Kill You!")
end

Now this makes the npc when it get's in combat yell "I Will Kill You!" and all races can understand

Now we need to register the function so hit enter a couple times and add

Code:
RegisterUnitEvent(YourNpcSpawnIDGoesHere, 1, "Nameofthefunction")

for "Nameofthefunction i'll put Sharptooth, and the 1 is the code for when this function will register

Here are other choices to put

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

Now we need to add the npc's spawn ID can be found at WoWhead or in game with .npc info "Entry ID=spawn ID" my Npc's spawn ID is "11011", and now well add this to the script


Code:
function Sharptooth_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "I Will Kill You!")
end

RegisterUnitEvent(11011, 1, "Sharptooth_OnCombat")

ou also must add these functions to the script or else it won't work


Code:
function Sharptooth_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents() 
end

function Sharptooth_OnKilledTarget(Unit, Event)
end

function Sharptooth_OnDied(Unit, Event)
Unit:RemoveEvents()
end

the "Remove Units"is used when you make a huge boss script with changing display ID's/scale ect. then you want your npc to go back to how it was at the begining of the fight. so you add

Code:
Unit:RemoveEvents()

The Sharptooth_OnKIlledTarget is used so when your npc kills the party or you, you can make it say something simply just add a

Unit:SendChatMessage(14, 0, "I Will Kill You!") in between the function and end ex:


Code:
function Sharptooth_OnKilledTarget(Unit, Event)
Unit:SendChatMessage(14, 0, "Come back when your worthy.")
end

And then to register these functions, again so all the functions you made get registerd(so they work)

Code:
RegisterUnitEvent(YourNpcSpawnIDGoesHere, 2, "_OnLeaveCombat")
RegisterUnitEvent(YourNpcSpawnIDGoesHere, 3, "_OnKilledTarget")
RegisterUnitEvent(YourNpcSpawnIDGoesHere, 4, "_OnDied")

Now we will put this script all together nicely:

function Sharptooth_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "I Will Kill You!")
end

function Sharptooth_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end

function Sharptooth_OnKilledTarget(Unit, Event)
Unit:SendChatMessage(14, 0, "Come back when your worthy.")
end

function Sharptooth_OnDied(Unit, Event)
Unit:RemoveEvents()
end

RegisterUnitEvent(11011, 1, "Sharptooth_OnCombat")
RegisterUnitEvent(11011, 2, "Sharptooth_OnLeaveCombat")
RegisterUnitEvent(11011, 3, "Sharptooth_OnKilledTarget")
RegisterUnitEvent(11011, 4, "Sharptooth_OnDied")

Congratualtions you have now scripted a npc :D i will decide if i should make further guides on LUA scripting after seeing this one rated. thx and hope you learned how to LUA script ^_^
 
Runescape Gamblingrunescape gambling
Back
Top