How to create NPC

Well, I thought it would be a nice idea to show people how to create Npc’s to sell and buy stuff. Also say stuff.

Here you will learn how to make your Npc do the following:

  • Greet Sentence
  • Sell Runes
  • Sell Items
  • Buy
  • Putting it together

Credits: – Saporaf (Tutorial) – YurOTS (Scripts)

We are going to start off with a basic Npc script.
In pretty much All servers there is a npc called “Mad”. Im going to take that script and show you alot of ways to change it.

If you go under data/npc/scripts/loot.lua you will get this:

Code:

Quote:—ox = 44—oy = 5—oz = 6 focus = 0 talk_start = 0 target = 0 following = false attacking = false function onThingMove(creature, thing, oldpos, oldstackpos) end function onCreatureAppear(creature) end function onCreatureDisappear(cid, pos) if focus cid then selfSay('Good bye then.') focus = 0 talk_start = 0 end end function onCreatureTurn(creature) end function msgcontains(txt, str) return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)')) end function onCreatureSay(cid, type, msg) msg = string.lower(msg) if ((string.find(msg, '(%a*)hi(%a*)')) and (focus 0)) and getDistanceToCreature(cid) < 4 then selfSay(‘Hello ’ .. creatureGetName(cid) .. ’! I buy swords, clubs, axes, helmets, boots, legs, shields and armors.’) focus = cid talk_start = os.clock() elseif string.find(msg, ’(%a*)hi(%a*)’) and (focus ~= cid) and getDistanceToCreature(cid) < 4 then selfSay(‘Sorry, ’ .. creatureGetName(cid) .. ’! I talk to you in a minute.’) elseif focus cid then talk_start = os.clock() if msgcontains(msg, 'royal helmet') then sell(cid,3330,1,20000) elseif msgcontains(msg, 'warrior helmet') then sell(cid,3307,1,3000) elseif msgcontains(msg, 'crusader helmet') then sell(cid,3329,1,3000) elseif msgcontains(msg, 'crown helmet') then sell(cid,3323,1,2000) elseif msgcontains(msg, 'devil helmet') then sell(cid,3294,1,2000) elseif msgcontains(msg, 'mystic turban') then sell(cid,3512,1,500) elseif msgcontains(msg, 'chain helmet') then sell(cid,3290,1,35) elseif msgcontains(msg, 'iron helmet') then sell(cid,3291,1,30) elseif msgcontains(msg, 'helmets') then selfSay('I buy royal (20k), warrior (3k), crusader (3k), crown (2k), devil (2k), chain (35gp) and iron helmets (30gp), also mystic turbans (500gp).') elseif msgcontains(msg, 'steel boots') and focus cid then sell(cid,3492,1,20000) elseif msgcontains(msg, ‘boh’) or msgcontains(msg, ‘boots of haste’) and focus cid then sell(cid,3017,1,4000) elseif msgcontains(msg, 'boots') then selfSay('I buy steel boots (20k) and boots of haste (4k).') elseif msgcontains(msg, 'magic plate armor') or msgcontains(msg, 'mpa') then sell(cid,3304,1,100000) elseif msgcontains(msg, 'dragon scale mail') or msgcontains(msg, 'dsm') then sell(cid,3324,1,30000) elseif msgcontains(msg, 'golden armor') then sell(cid,3298,1,10000) elseif msgcontains(msg, 'crown armor') then sell(cid,3319,1,5000) elseif msgcontains(msg, 'knight armor') then sell(cid,3308,1,3000) elseif msgcontains(msg, 'blue robe') then sell(cid,3505,1,3000) elseif msgcontains(msg, 'lady armor') then sell(cid,3332,1,1000) elseif msgcontains(msg, 'plate armor') then sell(cid,3295,1,300) elseif msgcontains(msg, 'brass armor') then sell(cid,3297,1,200) elseif msgcontains(msg, 'chain armor') then sell(cid,3296,1,100) elseif msgcontains(msg, 'armors') then selfSay('I buy golden (10k), crown (5k), knight (3k), lady (1k), plate (300gp), brass (200gp) and chain armors (100gp), also mpa (100k), dsm (30k) and blue robes (3k).') elseif msgcontains(msg, 'golden legs') then sell(cid,3302,1,80000) elseif msgcontains(msg, 'crown legs') then sell(cid,3320,1,5000) elseif msgcontains(msg, 'knight legs') then sell(cid,3309,1,3000) elseif msgcontains(msg, 'plate legs') then sell(cid,3495,1,500) elseif msgcontains(msg, 'brass legs') then sell(cid,3310,1,100) elseif msgcontains(msg, 'chain legs') then sell(cid,3310,1,50) elseif msgcontains(msg, 'legs') then selfSay('I buy golden (80k), crown (5k), knight (3k), plate (500gp), brass (100gp) and chain legs (50gp).') elseif msgcontains(msg, 'shield of the mastermind') or msgcontains(msg, 'mms') then sell(cid,3352,1,80000) elseif msgcontains(msg, 'demon shield') then sell(cid,3358,1,40000) elseif msgcontains(msg, 'vampire shield') then sell(cid,3372,1,4000) elseif msgcontains(msg, 'medusa shield') then sell(cid,3374,1,3500) elseif msgcontains(msg, 'amazon shield') then sell(cid,3375,1,3000) elseif msgcontains(msg, 'crown shield') then sell(cid,3357,1,2000) elseif msgcontains(msg, 'tower shield') then sell(cid,3366,1,2000) elseif msgcontains(msg, 'dragon shield') then sell(cid,3354,1,1500) elseif msgcontains(msg, 'guardian shield') then sell(cid,3353,1,1200) elseif msgcontains(msg, 'beholder shield') then sell(cid,3356,1,1000) elseif msgcontains(msg, 'dwarven shield') then sell(cid,3363,1,100) elseif msgcontains(msg, 'shields') then selfSay('I buy demon (40k), vampire (4k), medusa (3.5k), amazon (3k), crown (2k), tower (2k), dragon (1.5k), guardian (1.2k), beholder (1k), and dwarven shields (100gp), also mms (80k)') elseif msgcontains(msg, 'giant sword') then sell(cid,3219,1,10000) elseif msgcontains(msg, 'bright sword') then sell(cid,3233,1,6000) elseif msgcontains(msg, 'ice rapier') then sell(cid,3222,1,4000) elseif msgcontains(msg, 'fire sword') then sell(cid,3218,1,3000) elseif msgcontains(msg, 'serpent sword') then sell(cid,3235,1,1500) elseif msgcontains(msg, 'spike sword') then sell(cid,3209,1,800) elseif msgcontains(msg, 'two handed sword') then sell(cid,3203,1,400) elseif msgcontains(msg, 'sabre') then sell(cid,3211,1,25) elseif msgcontains(msg, 'sword') then sell(cid,3202,1,25) elseif msgcontains(msg, 'swords') then selfSay('I buy giant (10k), bright (6k), fire (3k) serpent (1.5k), spike (800gp) and two handed swords (400gp), also ice rapiers (4k), sabres (25gp) and swords (25gp).') elseif msgcontains(msg, 'dragon lance') then sell(cid,3240,1,10000) elseif msgcontains(msg, 'fire axe') then sell(cid,3258,1,10000) elseif msgcontains(msg, 'knight axe') then sell(cid,3256,1,2000) elseif msgcontains(msg, 'double axe') then sell(cid,3213,1,200) elseif msgcontains(msg, 'halberd') then sell(cid,3207,1,200) elseif msgcontains(msg, 'battle axe') then sell(cid,3204,1,100) elseif msgcontains(msg, 'hatchet') then sell(cid,3214,1,20) elseif msgcontains(msg, 'axes') then selfSay('I buy fire (10k), knight (2k), double (200gp) and battle axes (100gp), also dragon lances (10k), halberds (200gp) and hatchets (20gp).') elseif msgcontains(msg, 'war hammer') then sell(cid,3217,1,4000) elseif msgcontains(msg, 'skull staff') then sell(cid,3262,1,4000) elseif msgcontains(msg, 'dragon hammer') then sell(cid,3260,1,2000) elseif msgcontains(msg, 'clerical mace') then sell(cid,3249,1,200) elseif msgcontains(msg, 'battle hammer') then sell(cid,3243,1,60) elseif msgcontains(msg, 'mace') then sell(cid,3224,1,30) elseif msgcontains(msg, 'clubs') then selfSay('I buy war (4k), dragon (2k) and battle hammers (60gp), also skull staffs (4k) and clerical maces (200gp).') elseif msgcontains(msg, 'platinum amulet') then sell(cid,2993,1,2000) elseif msgcontains(msg, 'scarf') then sell(cid,3510,1,500) elseif msgcontains(msg, 'amulets') then selfSay('I buy platinum amulets (2k) and scarfs (500gp).') elseif string.find(msg, '(%a*)bye(%a*)') and getDistanceToCreature(cid) < 4 then selfSay('Good bye, ' .. creatureGetName(cid) .. '!') focus = 0 talk_start = 0 end end end function onCreatureChangeOutfit(creature) end function onThink() if (os.clock() - talk_start) > 30 then if focus > 0 then selfSay('Next Please...') end focus = 0 end if focus ~= 0 then if getDistanceToCreature(focus) > 5 then selfSay('Good bye then.') focus = 0 end end end

First of we will start with

Greeting Sentence

Where it says

Code:

if ((string.find(msg, '(%a*)hi(%a*)')) and (focus 0)) and getDistanceToCreature(cid) < 4 then selfSay(‘Hello ’ .. creatureGetName(cid) .. ’! I buy swords, clubs, axes, helmets, boots, legs, shields and armors.’)

On line 1 where it says “hi” this is what the player says when they want to talk with the Npc. I suggest to you not to change that.

Then where it says

Code:

selfSay(‘Hello ’ .. creatureGetName(cid) .. ’! I buy swords, clubs, axes, helmets, boots, legs, shields and armors.’)

After

Code:

selfSay(’

You can type whatever you want your Npc to say. Make sure that when your doen you add
Code:

’)

at the end.

Now if you have ’ (Aposturphies) in your sentce you always add
”\” befor it.

Example:

Code:

Hello, I’m a Npc

Should be like this:

Code:

Hello, I\’m a Npc

Now that you know that, we can go a little more advanced.

Buying

Now here is an Example of how to sell an Item.

Code:

elseif msgcontains(msg, ‘crusader helmet’) then sell(cid,3329,1,3000)

After where it says

Code:

(msg, ’[ITEM HERE]’)

You put what the item is called. This is what the player must say to sell this object to the Npc.

Where it says

Code:

(cid,3329,1,3000)

The “3329” is the item number that the Npc is buying from you. In this case its a Crusader Helmet.

Change this to the item you want the Npc to buy of you.

The “1” I suggest to you not to change that, because if the player only wants to sell 1 crusader helmet if you change that to 2 for example the Npc will buy 2 crusader helmets the player. It could also cause mix ups if the player only has 1 helmet.

Now the “3000” is the amount of gold coins the Npc will give the player for the helmet.

Next off:

Selling Runes

Now this is a little more difficult for this particular script.

Since this Npc only buys items you might not want to make it sell anything because players might get mixed up. So im going to take another Npc called “Dark Rodo”.

In data/npcs/scripts/rune.lua you will get this:

Code:
—Respawn position set—ox = 44—oy = 5—oz = 6 focus = 0 talk_start = 0 target = 0 following = false attacking = false function onThingMove(creature, thing, oldpos, oldstackpos) end function onCreatureAppear(creature) end function onCreatureDisappear(cid, pos) if focus cid then selfSay('Good bye then.') focus = 0 talk_start = 0 end end function onCreatureTurn(creature) end function msgcontains(txt, str) return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)')) end function onCreatureSay(cid, type, msg) msg = string.lower(msg) if ((string.find(msg, '(%a*)hi(%a*)')) and (focus 0)) and getDistanceToCreature(cid) < 4 then selfSay(‘Hello ’ .. creatureGetName(cid) .. ’! I sell hmms (40gps), uhs (40gps), gfbs (60gps), explosions (60gps), sds (90gps) and blank runes (5gps). You can also buy more runes, just say “10 uh” or “100 sd”.’) focus = cid talk_start = os.clock() elseif string.find(msg, ’(%a*)hi(%a*)’) and (focus ~= cid) and getDistanceToCreature(cid) < 4 then selfSay(‘Sorry, ’ .. creatureGetName(cid) .. ’! I talk to you in a minute.’) elseif msgcontains(msg, ‘100 hmm’) and focus cid then buy(cid,3136,100,800) talk_start = os.clock() elseif msgcontains(msg, '10 hmm') and focus cid then buy(cid,3136,10,80) talk_start = os.clock() elseif msgcontains(msg, ‘hmm’) and focus cid then buy(cid,3136,5,40) talk_start = os.clock() elseif msgcontains(msg, '100 uh') and focus cid then buy(cid,3098,100,4000) talk_start = os.clock() elseif msgcontains(msg, ‘10 uh’) and focus cid then buy(cid,3098,10,400) talk_start = os.clock() elseif msgcontains(msg, 'uh') and focus cid then buy(cid,3098,1,40) talk_start = os.clock() elseif msgcontains(msg, ‘100 gfb’) and focus cid then buy(cid,3129,100,2000) talk_start = os.clock() elseif msgcontains(msg, '10 gfb') and focus cid then buy(cid,3129,10,200) talk_start = os.clock() elseif msgcontains(msg, ‘gfb’) and focus cid then buy(cid,3129,3,60) talk_start = os.clock() elseif msgcontains(msg, '100 explosion') and focus cid then buy(cid,3138,100,2000) talk_start = os.clock() elseif msgcontains(msg, ‘10 explosion’) and focus cid then buy(cid,3138,10,200) talk_start = os.clock() elseif msgcontains(msg, 'explosion') and focus cid then buy(cid,3138,3,60) talk_start = os.clock() elseif msgcontains(msg, ‘100 sd’) and focus cid then buy(cid,3093,100,9000) talk_start = os.clock() elseif msgcontains(msg, '10 sd') and focus cid then buy(cid,3093,10,900) talk_start = os.clock() elseif msgcontains(msg, ‘sd’) and focus cid then buy(cid,3093,1,90) talk_start = os.clock() elseif msgcontains(msg, 'restoration') and focus cid then buy(cid,3095,1,100) talk_start = os.clock() elseif msgcontains(msg, ‘blank’) and focus cid then buy(cid,3085,1,5) talk_start = os.clock() elseif string.find(msg, '(%a*)bye(%a*)') and focus cid and getDistanceToCreature(cid) < 4 then selfSay(‘Good bye, ’ .. creatureGetName(cid) .. ’!’) focus = 0 talk_start = 0 end end function onCreatureChangeOutfit(creature) end function onThink() if (os.clock() – talk_start) > 30 then if focus > 0 then selfSay(‘Next Please…’) end focus = 0 end if focus ~= 0 then if getDistanceToCreature(focus) > 5 then selfSay(‘Good bye then.’) focus = 0 end end end

Now where it says

Code:

elseif msgcontains(msg, ‘100 hmm’) and focus cid then buy(cid,3136,100,800) talk_start = os.clock() elseif msgcontains(msg, '10 hmm') and focus cid then buy(cid,3136,10,80) talk_start = os.clock() elseif msgcontains(msg, ‘hmm’) and focus cid then buy(cid,3136,5,40) talk_start = os.clock()

This is for runes in this case it happens to be Hmm's (Heavy Magic Missile). This is a bit confusing for first timers, I am going to explain it as briefly as I can.

Here is part of the script just above.
Code:

elseif msgcontains(msg, 'hmm') and focus cid then buy(cid,3136,5,40) talk_start = os.clock()

Where it says “hmm” this is what the player must say to buy a hmm.

Now where it says

Code:

buy(cid,3136,5,40)

The ‘3136’ is the item id for hmm, you can change that to any number you want.

The ‘5’ is the number of shots perrune.

100 is the max you can go im pretty sure.

The ‘40’ is how many gold coins it will cost the player for the hmm.

Now where it says this:

Code:

elseif msgcontains(msg, ‘100 hmm’) and focus cid then buy(cid,3136,100,800) talk_start = os.clock() elseif msgcontains(msg, '10 hmm') and focus cid then buy(cid,3136,10,80) talk_start = os.clock()

If the player says ‘100 hmm’ they will get a hmm with 100 shots.

its the same as the script I just showed you.

the ‘100’ in the middle of

Code:

buy(cid,3136,100,800)

is how many shots they get.

Now we move on a bit more.

Selling Items

If you want your Npc to sell an item, it is pretty much the same thing as selling runes. Except you need to change a few things.

Code:

elseif msgcontains(msg, ‘hmm’) and focus == cid then buy(cid,3136,5,40) talk_start = os.clock()

We are going to take that and all you need to do is change the following:

1. Change the
Code:

(msg, ‘hmm’)

to the item name
2. Then the
Code:

buy(cid,3136,5,40)

to the 3136 to the item Id
3. Then the 5 to the number you want to give them (I suggest 1)
4. And last of the 40 to how much it cost

Now for this:

Putting it together

We are going to go ALL the way back to Mad. This time we are gonna go to data/npcs/mad.xml where you will see the following.

Code:

<?xml version=”1.0”?> <npc name=”Mad” script=”data/npc/scripts/loot.lua” access=”3” lookdir=”2”> <mana now=”800” max=”800”/> <health now=”200” max=”200”/> <look type=”129” head=”76” body=”114” legs=”124” feet=”86”/> </npc>

Where it says

Code:

<npc name=”Mad”

In the quatations (“”) put the Npcs name.

Then where it says

Code:

script=”data/npc/scripts/loot.lua”

After /scripts/put in what you saved your npcs script as. Remeber in the data/npc/scripts/(SCRIPT.LUA)

Now the Npcs Look

Where it says

Code:

<look type=”129” head=”76” body=”114” legs=”124” feet=”86”/>

You can change the Numbers (#) to the type you want them to be. I STRONGLY suggest that you just copy and paste the looks from differnt Npcs/monsters.

Then save it as (NPC.XML)

Related Articles
Paladin guide: Making money
Paladin guide: Professional hunting - levels 61-80
Paladin guide: Graduated hunting rights - levels 40-60
Paladin guide: Main - levels 9-40
Paladin guide: The Infamous Rookgaard - levels 1-8
How to train a Knight
How to become a good mapper
How to make conjured items
How to start Tibia 7.5 server
How to make cheat mode


Previous article: How to make a god char
Next article: Make PVP arena