Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Add-On] Learning NPC scripts (beginner's guide)

Status
Not open for further replies.
Initiate Mage
Joined
Feb 12, 2009
Messages
2
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Hey can u tell me whats wrong with this i just cant get it to work it would b a big help.

Sorry messed this 1 up so i reposted under it



var status = 0;

function start() {
status = -1;
action(1, 0, 0);
}

function action(mode, type, selection) {

if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}

if (mode == 1) {
status++;
}
else {
status--;
}

if (status == 0) {
cm.sendNext("hello, how are you?");
}
else if (status == 1) {
cm.sendSimple("Have you come to trade #b#v4000313##k\r\n#Lo#Yes, lets trade.#l\r\n#L1#Sorry, I did'nt mean to bother you.#l");

}
else if (status == 2) {
if (selection == 0) {
cm.sendSimple("Okay what will it be?\r\n#L0#3 #b#v4000313##k = #b#v1902002##k#l\r\n#L1#1 #b#v4000313##k = #b#v2040807##k#l");
}
else if (selection == 1) {
cm.sendOk("See you later");
cm.dispose();
}
else if (status == 3) {
if (selection == 0) {
if(cm.haveItem(4000313, 3)) {
cm.gainItem(1902002, 1);
cm.gainItem(4000313, -3);
cm.dispose();
}
else if (selection == 1) {
if(cm.haveitem(4000313, 1)) {
cm.gainItem(2040807, 1);
cm.gainItem(4000313, -1);
cm.dispose();
}
}
}
}
 
Initiate Mage
Joined
Feb 12, 2009
Messages
2
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Code:
var status = 0;   

function start() {   
    status = -1;   
    action(1, 0, 0);   
}   

function action(mode, type, selection) {   
        
    if (mode == -1) {   
        cm.dispose();   
    }   
    else {    
        if (status >= 2 && mode == 0) {    
            cm.sendOk("Goodbye");    
            cm.dispose();    
            return;    
        }    
           
        if (mode == 1) {   
            status++;   
        }       
        else {   
            status--;   
        }   
           
        if (status == 0) {   
          cm.sendNext("hello, how are you?");
        }
        else if (status == 1) {   
            cm.sendSimple("Have you come to trade #b#v4000313##k\r\n#Lo#Yes, lets trade.#l\r\n#L1#Sorry, I did'nt mean to bother you.#l");
        
        }
        else if (status == 2) {   
            if (selection == 0) {   
                cm.sendSimple("Okay what will it be?\r\n#L0#3 #b#v4000313##k = #b#v1902002##k#l\r\n#L1#1 #b#v4000313##k = #b#v2040807##k#l");
            }
            else if (selection == 1) {   
                cm.sendOk("See you later");
                cm.dispose();
        }
        else if (status == 3) {   
            if (selection == 0) {   
            if(cm.haveItem(4000313, 3)) {   
                cm.gainItem(1902002, 1);
                cm.gainItem(4000313, -3);
                cm.dispose();
            }
            else if (selection == 1) {   
                if(cm.haveitem(4000313, 1)) {   
                    cm.gainItem(2040807, 1);
                    cm.gainItem(4000313, -1);
                    cm.dispose();
            }
        }
    }
}
 
Initiate Mage
Joined
Apr 26, 2008
Messages
3
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

PHP:
// Filleone
// First NPC

var status = 0;

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {

    if (mode == -1) {
        cm.dispose();
    }
    else {
        if (status >= 2 && mode == 0) {
            cm.sendOk("See you around, handsome! ;)");
            cm.dispose();
            return;
        }

        if (mode == 1) {
            status++;
        }
        else {
            status--;
        }

        if (status == 0) {

                        cm.sendNext("Hello, I'm Sera!");
            cm.dispose();
        }
        else if (status == 1) {
            cm.sendNext("I help newcomers, like you!");
            cm.dispose();
            }
        if (status == 0) {
            cm.sendAcceptDecline("Here's a Beta #t1002518, you only get one, so take care of it!");
        }
    else if (status == 1) {
        if(cm.haveItem(1002518)) {
        cm.sendOk ("You already have a #t1002518 I'm not stupid!");
        cm.dispose ();
    }
    else {
        cm.sendOk("Here's your #t1002518# and some Mesos & NX! Good luck on your adventure!");
        cm.gainItem(1002518, 1);
        cm.gainMeso(10000);
        gm.gainNX(10000);
        cm.warp([000040000], [0]);
        cm.dispose();

    }



    }

    }

}
Instant d/c when you click the npc, any help? I can't see what I did wrong, when I compare to the guide. Please try to explain what I did wrong, don't just correct it. :)

Very nice guide, easy to follow.
 
Last edited:
Junior Spellweaver
Joined
Jul 21, 2008
Messages
124
Reaction score
47
Re: [Tut] Learning NPC scripts (beginner's guide)

@ filleone

Read the comments in the script

PHP:
// Filleone
// First NPC

var status = 0;

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {

    if (mode == -1) {
        cm.dispose();
    }
    else {
        if (status >= 2 && mode == 0) {
            cm.sendOk("See you around, handsome! ;)");
            cm.dispose();
            return;
        }

        if (mode == 1) {
            status++;
        }
        else {
            status--;
        }

        if (status == 0) {
            cm.sendNext("Hello, I'm Sera!"); // do not add cm.dispose unless the conversation is ending there
        }
        else if (status == 1) {
            cm.sendNext("I help newcomers, like you!"); // do not add cm.dispose unless the conversation is ending there
        }
        else if (status == 2) { // status should continue going UP 1,2,3,4, etc.
            cm.sendAcceptDecline("Here's a Beta #t1002518, you only get one, so take care of it!");
        }
        else if (status == 3) { // status should continue going UP 1,2,3,4, etc.
            if(cm.haveItem(1002518)) {
                cm.sendOk ("You already have a #t1002518 I'm not stupid!");
                cm.dispose(); // no space in cm.dispose();
            }
            else {
                cm.sendOk("Here's your #t1002518# and some Mesos & NX! Good luck on your adventure!");
                cm.gainItem(1002518, 1);
                cm.gainMeso(10000);
                cm.gainNX(10000); //cm.gainNX (Sorry that was a typo in the guide)
                cm.warp(000040000, 0); // the [] in the guide aren't used when you code
                cm.dispose();
            }
        }
    }
}

@ Dana

PHP:
var status = 0;   

function start() {   
    status = -1;   
    action(1, 0, 0);   
}   

function action(mode, type, selection) {   
        
    if (mode == -1) {   
        cm.dispose();   
    }   
    else {    
        if (status >= 2 && mode == 0) {    
            cm.sendOk("Goodbye");    
            cm.dispose();    
            return;    
        }    
           
        if (mode == 1) {   
            status++;   
        }       
        else {   
            status--;   
        }   
           
        if (status == 0) {   
            cm.sendNext("hello, how are you?");
        }
        else if (status == 1) {   
            cm.sendSimple("Have you come to trade #b#v4000313##k\r\n#L0#Yes, lets trade.#l\r\n#L1#Sorry, I did'nt mean to bother you.#l"); // L0 not Lo
        }
        else if (status == 2) {   
            if (selection == 0) {   
                cm.sendSimple("Okay what will it be?\r\n#L2#3 #b#v4000313##k = #b#v1902002##k#l\r\n#L3#1 #b#v4000313##k = #b#v2040807##k#l"); // You can't repeat the selection number. After you use selection 1 and 2 use the next ones with 3,4, etc.
            }
            else if (selection == 1) {   
                cm.sendOk("See you later");
                cm.dispose();
            }
        }
        else if (status == 3) {   
            if (selection == 2) {   
                if(cm.haveItem(4000313, 3)) {   
                    cm.gainItem(1902002, 1);
                    cm.gainItem(4000313, -3);
                    cm.dispose();
                }
                else { // You need an else option if the person doesn't have the 4000313 (which I believe is the golden maple leaf)
                    cm.sendOk("You don't have enough #v4000313#");
                    cm.dispose();
                }
            }
            else if (selection == 3) {   
                if(cm.haveItem(4000313, 1)) {   
                    cm.gainItem(2040807, 1);
                    cm.gainItem(4000313, -1);
                    cm.dispose();
                }
                else { // Same reason 
                    cm.sendOk("You don't have enough #v4000313#");
                    cm.dispose();
                } // added to end else { 
            }
        }
    }
}
 
Initiate Mage
Joined
Apr 26, 2008
Messages
3
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Thank you :) I'll get a hang of it some day. ;-P

Edit: I used your correction. But the npc does'nt warp you. Any ideas?
 
Last edited:
Junior Spellweaver
Joined
Jul 21, 2008
Messages
124
Reaction score
47
Re: [Tut] Learning NPC scripts (beginner's guide)

@ filleone

Change it to

cm.warp(40000, 0);
 
Initiate Mage
Joined
Apr 26, 2008
Messages
3
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Works great now! Thanks for an awesome guide!
 
Newbie Spellweaver
Joined
Jul 17, 2008
Messages
32
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Can u show me an example of takin an item away and how do i get my npc on my server im newby at this
 
Junior Spellweaver
Joined
Jul 21, 2008
Messages
124
Reaction score
47
Re: [Tut] Learning NPC scripts (beginner's guide)

@ calvin21

To take an item away just change the number in the cm.gainItem to a negative.

Script your NPC and put the .js into your npc folder in the repack. Rename it whatever your NPC id is. Then you can use !pnpc to permanetly place the NPC somewhere.
 
Newbie Spellweaver
Joined
Feb 15, 2009
Messages
45
Reaction score
2
Re: [Tut] Learning NPC scripts (beginner's guide)

NVM XD You got any idea how to do a PQ script?
 
Last edited:
Newbie Spellweaver
Joined
Nov 21, 2008
Messages
10
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

how can I make a script like a Party Quest(the party leader need to talk to the NPC)and need minimum 3 members?
 
Junior Spellweaver
Joined
Jul 21, 2008
Messages
124
Reaction score
47
Re: [Tut] Learning NPC scripts (beginner's guide)

@vincenty2x2

PHP:
if (cm.getParty().getMembers().size() > 3 || party.size() < 6) {

If the party is the size of more than 3 and less than 6...

Got this out of ThePack's script
 
Newbie Spellweaver
Joined
Feb 14, 2009
Messages
7
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Let's say I wanted a GM roar teacher NPC, would I use this?

Code:
var status = 0;

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1)
        cm.dispose();
    else {
        if (mode == 0 && status == 0)
            cm.dispose();
        if (mode == 1)
            status++;
        else
            status--;
        if (status == 0) {
            cm.sendSimple("Hello, I can teach you the skill GM Roar.\r\n#L1# I would like to trade in 15,000 #b#v4001024##k for GM Roar!#l\r\n\#L2# I would like to trade in 750 #b#v4000352##k for GM Roar.#l");
        } else if (status == 1) {
            if (selection == 1) {
                if(cm.haveItem(4001024,15000)) {
                    cm.teachSkill(5001001,1,1);
                    cm.gainItem(4001024,-15000);
		    cm.changeJob(900);
	            cm.sendOk("You have learned GM Roar! Congrats! If you did not learn GM Roar please contact a GM.");
                } else
		    cm.sendOk("Sorry, you don't have 15,000 #b#v4001024##k!");
		cm.dispose();
	    } else if (selection == 2) {
		if(cm.haveItem(4000352,750)) {
		    cm.teachSkill(5001001,1,1);
		    cm.gainItem(4000352,-750);
		    cm.changeJob(900);
		    cm.sendOk("You have learned GM Roar! Congrats! If you did not learn GM Roar please contact a GM.");
		} else
		    cm.sendOk("Sorry, you don't have 750 #b#v4000352##k!");
		cm.dispose();
	    }
	    else
	        cm.sendOk("See you later!");
        }
    }
}
 
Junior Spellweaver
Joined
Jul 21, 2008
Messages
124
Reaction score
47
Re: [Tut] Learning NPC scripts (beginner's guide)

@ iDevil

You're a great scripter! You're just missing a couple things here and there. You don't do an }else{ at the bottom of the script. I know that you're trying to make an option for end chat.
PHP:
var status = 0;  

function start() {  
    status = -1;  
    action(1, 0, 0);  
}  

function action(mode, type, selection) {  
    if (mode == -1) {  
        cm.dispose();  
    }  
    else {   
        if (status >= 2 && mode == 0) {   
            cm.sendOk("See you later!"); // end chat
            cm.dispose();   
            return;   
        }    
        if (mode == 1) {  
            status++;  
        }      
        else {  
            status--;  
        }  
        if (status == 0) { 
            cm.sendSimple("Hello, I can teach you the skill GM Roar.\r\n#L1# I would like to trade in 15,000 #b#v4001024##k for GM Roar!#l\r\n\#L2# I would like to trade in 750 #b#v4000352##k for GM Roar.#l");
        } else if (status == 1) {
            if (selection == 1) {
                if(cm.haveItem(4001024,15000)) {
                    cm.teachSkill(5001001,1,1);
                    cm.gainItem(4001024,-15000);
		            cm.changeJob(900);
	                cm.sendOk("You have learned GM Roar! Congrats! If you did not learn GM Roar please contact a GM.");
	                cm.dispose(); // You need cm.dispose();
                } 
                else { // Missing { here
		            cm.sendOk("Sorry, you don't have 15,000 #b#v4001024##k!");
		            cm.dispose();
	        } 
            else if (selection == 2) {
		        if(cm.haveItem(4000352,750)) {
		            cm.teachSkill(5001001,1,1);
		            cm.gainItem(4000352,-750);
		            cm.changeJob(900);
		            cm.sendOk("You have learned GM Roar! Congrats! If you did not learn GM Roar please contact a GM.");
		            cm.dispose(); // You need cm.dispose();
                } 
                else { // Missing { here
		            cm.sendOk("Sorry, you don't have 750 #b#v4000352##k!");
		            cm.dispose();
                } // ends else {
            } // ends (selection == 2) {
        } // ends (status == 1) {
    } // ends else { at the top of the script
} // ends (mode, type, selection) {
 
Newbie Spellweaver
Joined
Feb 15, 2009
Messages
45
Reaction score
2
Re: [Tut] Learning NPC scripts (beginner's guide)

@vincenty2x2

PHP:
if (cm.getParty().getMembers().size() > 3 || party.size() < 6) {

If the party is the size of more than 3 and less than 6...

Got this out of ThePack's script

And how do you make it check if they are lvl 120-180?

PHP:
if (cm.getParty().getMembers().size() > 3 || party.size() < 6 && cm.getLevel() >= 120 =< 180 ) {

This way?

Oooh,the getLevel function made the server crash :&
 
Last edited:
Newbie Spellweaver
Joined
Feb 4, 2009
Messages
8
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

var status = 0;

function start() {
status= -1;
action(1, 0, 0);
}

function action(mode, type, selection) {

if (mode == -1) {
cm.dispose() ;
}
else {
if (status >=2 && mode == 0) {
cm.sendOk("Goodbye") ;
cm.dispose() ;
return;
}

if (mode == 1) {
status++;
}
else {
status--;
}

if (status == 0) {
cm.sendSimple("Hey, wanna buy special items. \r\n#L0#wizet hat#l \r\n#L1# super uber weapon.#l") ;
}
else if (status == 1) {
if (selection == 0) {
if (cm.haveItem(4031543, 500) ) {
cm.gainItem (4031543, -500) ;
cm.gainItem (1002140, 1) ;
cm.dispose() ;
}
}
else {
cm.sendOk ("come back when you have enough ticks")
cm.dispose() ;
}
else if (status == 1) {
if (selection == 1) {
if (cm.haveItem(4031543, 500) ) {
cm.gainItem (1472051, 1) ;
cm.gainItem (4031543, -500) ;
cm.dispose() ;
}
}
else {
cm.sendOk ("come back when you have enough ticks")
cm.dispose() ;
}
}
}
}


would this script work why or why not (its my first script).
Btw thx for the tut it was what made me learn the stuff about { and } and scripting npcs :)
 
Last edited:
Newbie Spellweaver
Joined
Feb 4, 2009
Messages
8
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

erm wth...it made me post twice 0.o
 
Last edited:
Initiate Mage
Joined
Jun 16, 2008
Messages
1
Reaction score
0
Re: [Tut] Learning NPC scripts (beginner's guide)

Can someone show me how to use (if(cm.getChar().isDonator() == true) ) in a YesNo npc
 
Newbie Spellweaver
Joined
Sep 30, 2008
Messages
27
Reaction score
1
Re: [Tut] Learning NPC scripts (beginner's guide)

this is a good guide for some people but for beginners, they have no idea what level 1 stuff is, you should have got into depth about the status and whatnot
 
Junior Spellweaver
Joined
Jul 21, 2008
Messages
124
Reaction score
47
Re: [Tut] Learning NPC scripts (beginner's guide)

@ beebeewawa

it should be else if (selection == 1) {

@ vertskater12

Thanks for the advice. Level 1 is very short and really only introduces the beginning of a script. I chose not to go indepth with "the status and wahtnot" because I want this script to teach how to code well. There's indepth explanations in other guides if you really want to get one.
 
Status
Not open for further replies.
Back
Top