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!

[Solved]Npc script help

Newbie Spellweaver
Joined
Jun 16, 2010
Messages
23
Reaction score
1
I'm relatively new to NPC scripting. What I'm trying to do here is make an NPC that will check if you have a certain number of Miracle Cubes. If you have the required number, the NPC will take away that number of Miracle Cubes and give you a certain number of mesos based on the amount of Cubes you gave. But right now each selection says "You do not have 1 #i5062000#." except for the first selection which will say ""You just traded 1 #i5062000# for 2,000,000 mesos!" regardless of whether or not you actually have cubes. It doesn't give any mesos either.
Here's the code:

PHP:
var status;

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

function action (mode, type, selection) {
	if (mode == 1) {
	status++;
}else{
	status--;
}

if (status == 0) {
	cm.sendSimple("Yo! You can trade #i5062000# with me in exchage for mesos!\r\n#L0#Trade 1 #i5062000# for 2,000,000 mesos.\r\n#L1#Trade 5 #i5062000# for 10,000,000 mesos.\r\n#L2#Trade 10 #i5062000# for 20,000,000 mesos.\r\n#L3#Trade 50 #i5062000# for 100,000,000 mesos.");
} 
else if (status == 1) {

	if (selection == 0) { 
	   if (cm.haveItem(5062000, 1)){
		cm.gainItem(5062000, -1);
		cm.gainMeso(2000000);
		cm.sendOk("You just traded 1 #i5062000# for 2,000,000 mesos!");}
		else{
		cm.sendOk("You do not have 1 #i5062000#.");}
		cm.dispose();
		}
	if (selection == 1) { 
		if (cm.haveItem(5062000, 5)){
		cm.gainItem(5062000, -5);
		cm.gainMeso(10000000);
		cm.sendOk("You just traded 5 #i5062000# for 10,000,000 mesos!");}
		else{
		cm.sendOk("You do not have 5 #i5062000#.");}
		cm.dispose();		
		}
	if (selection == 2) { 
		if (cm.haveItem(5062000, 10)){
		cm.gainItem(5062000, -10);
		cm.gainMeso(20000000);
		cm.sendOk("You just traded 10 #i5062000# for 20,000,000 mesos!");}
		else{
		cm.sendOk("You do not have 10 #i5062000#.");}
		cm.dispose();
		}
	if (selection == 3) { 
		if (cm.haveItem(5062000, 50)){
		cm.gainItem(5062000, -50);
		cm.gainMeso(100000000);
		cm.sendOk("You just traded 50 #i5062000# for 100,000,000 mesos!");}
		else{
		cm.sendOk("You do not have 50 #i5062000#.");}
		cm.dispose();
		}
}else{
	cm.dispose();
}
}

Never mind, I tweaked the brackets a little and now it works. The code above is a working script now, if anyone wants to use it. :)
 
Last edited:
Newbie Spellweaver
Joined
May 7, 2010
Messages
83
Reaction score
9
Re: Npc script help

I'm a noob to NPC scripting as well, I don't really know what your problem is, but I'm not seeing you use cm.dispose(); much. For example... you have
PHP:
if (selection == 1) { 
        if (cm.haveItem(5062000, 5)){
        cm.gainItem(5062000, -5);
        cm.gainMeso(10000000);
        }
        cm.sendOk("You just traded 5 #i5062000# for 10,000,000 mesos!");
        }else{
        cm.sendOk("You do not have 5 #i5062000#.");
        }
I'm pretty sure you need cm.dispose(); after those, so it should be
PHP:
if (selection == 1) { 
        if (cm.haveItem(5062000, 5)){
        cm.gainItem(5062000, -5);
        cm.gainMeso(10000000);
        cm.dispose(); //not too sure if it's needed here or not, but i think it is.
        }//also, why is there a bracket here?
        cm.sendOk("You just traded 5 #i5062000# for 10,000,000 mesos!");
        cm.dispose();
        }else{
        cm.sendOk("You do not have 5 #i5062000#.");
        cm.dispose();
        }
 
Upvote 0
Junior Spellweaver
Joined
Oct 5, 2011
Messages
114
Reaction score
2
Re: Npc script help

I don't know if that is what you want:

PHP:
var status;

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

function action (mode, type, selection) {
    if (mode == 1) {
    status++;
}else{
    status--;
}

if (status == 0) {
	if (cm.haveItem(5062000)) {
		cm.sendSimple("Yo! You can trade #i5062000# with me in exchage for mesos!\r\n#L0#Trade 1 #i5062000# for 2,000,000 mesos.\r\n#L1#Trade 5 #i5062000# for 10,000,000 mesos.\r\n#L2#Trade 10 #i5062000# for 20,000,000 mesos.\r\n#L3#Trade 50 #i5062000# for 100,000,000 mesos.");
	}else{
		cm.sendOk("You do not have any #i5062000# #t5062000#. You may hunt for them by speaking to NPC Chef in the FM Entrance.");
		cm.dispose();
} 
}else if (status == 1) {
    if (selection == 0) { 
       if (cm.haveItem(5062000, 1)){
        cm.gainItem(5062000, -1);
        cm.gainMeso(2000000);
        cm.sendOk("You just traded 1 #i5062000# for 2,000,000 mesos!");}
        else{
        cm.sendOk("You do not have 1 #i5062000#.");}
        cm.dispose();
        }
    if (selection == 1) { 
        if (cm.haveItem(5062000, 5)){
        cm.gainItem(5062000, -5);
        cm.gainMeso(10000000);
        cm.sendOk("You just traded 5 #i5062000# for 10,000,000 mesos!");
		}else{
        cm.sendOk("You do not have 5 #i5062000#.");}
        cm.dispose();        
        }
    if (selection == 2) { 
        if (cm.haveItem(5062000, 10)){
        cm.gainItem(5062000, -10);
        cm.gainMeso(20000000);
        cm.sendOk("You just traded 10 #i5062000# for 20,000,000 mesos!");
        }else{
        cm.sendOk("You do not have 10 #i5062000#.");}
        cm.dispose();
        }
    if (selection == 3) { 
        if (cm.haveItem(5062000, 50)){
        cm.gainItem(5062000, -50);
        cm.gainMeso(100000000);
        cm.sendOk("You just traded 50 #i5062000# for 100,000,000 mesos!");
        }else{
        cm.sendOk("You do not have 50 #i5062000#.");}
        cm.dispose();
        }
	}
}
 
Upvote 0
Newbie Spellweaver
Joined
May 7, 2010
Messages
83
Reaction score
9
Re: Npc script help

Actually, I have managed to rewrite this. It's probably not the most organized or whatnot, but it works for me. I replaced the item id with a pigmy egg since I'm on v83 doing this.

PHP:
var status;

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

function action(mode, type, selection) {
    if (mode == 1) {
        status++;
    }else{
        status--;
    }

    if (status == 0) {
        cm.sendSimple("Yo! You can trade #i4170007# with me in exchage for mesos!\r\n#L0#Trade 1 #i4170007# for 2,000,000 mesos.#l\r\n#L1#Trade 5 #i4170007# for 10,000,000 mesos.#l\r\n#L2#Trade 10 #i4170007# for 20,000,000 mesos.#l\r\n#L3#Trade 50 #i4170007# for 100,000,000 mesos.#l");
    } else if (status == 1) {
        if (selection == 0) {
            if (cm.haveItem(4170007, 1)) {
                cm.gainItem(4170007, -1);
                cm.gainMeso(2000000);
                cm.sendOk("Enjoy!!");
                cm.dispose();
            }else{
                cm.sendOk("You do not have 1 #i4170007#.");
                cm.dispose();
            }
        } else if (selection == 1) {
            if (cm.haveItem(4170007, 5)) {
                cm.gainItem(4170007, -5);
                cm.gainMeso(10000000);
                cm.sendOk("Enjoy!!");
                cm.dispose();
            }else{
                cm.sendOk("You do not have 1 #i4170007#.");
                cm.dispose();
            }
            } else if (selection == 2) {
            if (cm.haveItem(4170007, 10)) {
                cm.gainItem(4170007, -10);
                cm.gainMeso(20000000);
                cm.sendOk("Enjoy!!");
                cm.dispose();
            }else{
                cm.sendOk("You do not have 1 #i4170007#.");
                cm.dispose();
           }
            } else if (selection == 3) {
               if (cm.haveItem(4170007, 50)) {
                cm.gainItem(4170007, -50);
                cm.gainMeso(100000000);
                cm.sendOk("Enjoy!!");
                cm.dispose();
            }else{
                cm.sendOk("You do not have 1 #i4170007#.");
                cm.dispose();
            }
        }
    }
}
 
Upvote 0
bleh....
Loyal Member
Joined
Oct 15, 2008
Messages
2,898
Reaction score
1,129
Re: Npc script help

Ok, been awhile since I've done this, so here goes..


Sorry... apparently PHP tags don't work the same as they used to? If not, here:

Code:
var cube = 5062000;var cubeAmt = [1, 5, 10, 50];
var mesos = [2000000, 10000000, 20000000, 100000000];


function start() {
    var text = "Yo! You can trade #i"+cube+"# with me in exchage for mesos!";
    for (var i = 0; i < mesos.length; i++)
    text += "\r\n#L"+i+"# Trade "+cubeAmt[i]+" #i"+cube+"# for "+mesos[i]+" mesos#l";
    cm.sendSimple(text);
    }
    
function action (mode, type, selection) {
    if (mode != 1) {
        cm.dispose();
    } else {
        if (cm.haveItem(cube, cubeAmt[selection)) {
            cm.gainItem(cube, -cubeAmt[selection));
            cm.gainMeso(mesos[selection]);
            cm.sendOk("You just traded "+cubeAmt[selection]+" #i"+cube+"# for "+mesos[selection]+" mesos!");
        } else {
            cm.sendOk("You do not have "+cubeAmt[selection]+" #i"+cube+"#.");
        }
        cm.dispose();
    }
}


Let me explain how it works.


This snippet here:

PHP:
for (var i = 0; i < mesos.length; i++)          text += "\r\n#L"+i+"# Trade "+cubeAmt[i]+" #i"+cube+"# for "+mesos[i]+" mesos#l";


Basically, "i" (random letter, can be anything tbh) is 0. "i" is less than mesos.length. If you look at the mesos array up top, there are 4 different amounts. You can look at the array as this as well.

PHP:
var mesos = [0, 1, 2, 3];

So, when this loop runs, it will continue to cycle through the array, until "i" is greater than or equal to the arrays length. The mesos array has a length of 3 (4 if you want to be technical since 0 is a part of it). So let's do that long form, and I'll show you what it will output for each cycle.


Cycle 1:

Code:
var cube = 5062000;
var cubeAmt = [[U][B]1[/B][/U], 5, 10, 50];
var mesos = [[U][B]2000000[/B][/U], 10000000, 20000000, 100000000];

for (var i = 0; i < mesos.length; i++)          text += "\r\n#L"+i+"# Trade "+cubeAmt[i]+" #i"+cube+"# for "+mesos[i]+" mesos#l";

Cycle 2:

Code:
var cube = 5062000;
var cubeAmt = [1, [U][B]5[/B][/U], 10, 50];
var mesos = [2000000, [U][B]10000000[/B][/U], 20000000, 100000000];

for (var i = 0; i < mesos.length; i++) 
         text += "\r\n#L"+i+"# Trade "+cubeAmt[i]+" #i"+cube+"# for "+mesos[i]+" mesos#l";



And so on and so forth. It's a much shorter way of doing selections. Now, you're probably wondering what this whole crap at the bottom means..


PHP:
if (cm.haveItem(cube, cubeAmt[selection)) {            cm.gainItem(cube, -cubeAmt[selection));            cm.gainMeso(mesos[selection]);

Well, like I said, the for loop is a shorter way of doing the selections. So, how I had shown you the arrays can also be viewed as:

PHP:
var mesos = [0, 1, 2, 3];

Think of the length as the selection. If you chose selection 0, then you can view the last part as this:

PHP:
if (cm.haveItem(cube, cubeAmt[0)) {            cm.gainItem(cube, -cubeAmt[0));            cm.gainMeso(mesos[0]);

Or... in a very long sense:

PHP:
if (cm.haveItem(5062000, 1)) {            cm.gainItem(5062000, -1);            cm.gainMeso(2000000);


The benefit of the forloop, is you can place all of your selections into a bundle like I have, rather than having to type out many selections and conditions. Hope I explained it enough. If not, check out my guide:


http://forum.ragezone.com/f428/add-learning-npcs-start-finish-643364/
 
Upvote 0
Custom Title Activated
Loyal Member
Joined
Aug 21, 2009
Messages
1,149
Reaction score
598
Re: Npc script help

Shawn, the length is 4. The index goes from 0~3. But everything else, well done. :D
 
Upvote 0
Newbie Spellweaver
Joined
Jun 16, 2010
Messages
23
Reaction score
1
Re: Npc script help

Thanks for that Shawn. In the past few weeks I've learned a lot from your NPC scripting guide, and now I've learned even more from your post. I'm not too sure on using variables, in the sense of how they're structured. For example when you used:

PHP:
var cube = 5062000;var cubeAmt = [1, 5, 10, 50];
var mesos = [2000000, 10000000, 20000000, 100000000];

Would I be correct in saying that "cube", "cubeAmt", and "mesos" are the substitutes for the actual values which are listed afterward?
 
Upvote 0
Back
Top