Author Topic: Setting an item to slot 4.  (Read 981 times)

I'm not all that advanced in scripting and I'm trying to get into it. One problem I have encountered trying to make a little script is that I have no idea how set and item to a specific slot. Could someone give me an example of what the code should look like?

Do you mean to give the player a tool in slot 4?
Code: [Select]
%item = hammerItem.getID();//the item you want to give them, change it to yours
%slot = 3;//the index of the slot
%oldTool = %player.tool[%slot];
%player.tool[%slot] = %item;
messageClient(%player.client,'MsgItemPickup','',%slot,%item);
if(%oldTool <= 0)
%player.weaponCount++;
« Last Edit: April 20, 2011, 09:20:50 PM by Destiny/Zack0Wack0 »

Do you mean to give the player a tool in slot 4?
Code: [Select]
%item = hammerItem.getID();//the item you want to give them, change it to yours
%slot = 3;//the index of the slot
%oldTool = %player.tool[%slot];
%player.tool[%slot] = %item;
messageClient(%player.client,'MsgItemPickup','',%slot,%item);
if(%oldTool <= 0)
%player.weaponCount++;
By "give" does it override the current item in the slot?

Yes, the old item would be replaced with the new one.

If you wanted it not to be able to do that, use an if statement
Code: [Select]
if(%oldTool <= 0)
   //Don't do anything
else
   //Zack's Script

Alright, I'm having an issue with that bit of code.
I'm testing it out and no matter what it doesn't add the weapon to my inventory. I set it to a command like...
Code: [Select]
function serverCmdgunswitchtest(%client,%player)
{
%item = gunItem.getID();
%slot = 1;
%oldTool = %player.tool[%slot];
%player.tool[%slot] = %item;
messageClient(%player.client,'MsgItemPickup','',%slot,%item);
if(%oldTool <= 0)
player.weaponCount++;
}
I type in /gunswitchtest and my inventory doesn't change.
« Last Edit: May 19, 2011, 01:17:04 AM by Daenth »

Code: [Select]
if(%oldTool <= 0)
%player.weaponCount++;
Not every item is a weapon.

Is %player actually a player object or are you just trying to get the client who used the command's player object?
If so, use %client.player

Is %player actually a player object or are you just trying to get the client who used the command's player object?
If so, use %client.player

Even if I switch, it won't work. =/

the %player in your command is never set to anything, and your script expects it to be set -- so its not going to work.

try this:
Code: [Select]
function serverCmdgunswitchtest(%client)
  {
   %player = %client.player;
   %slot = 1;
   if (!isObject(%client.oldTool))
     {
      %item = %player.oldTool;
     }
   else
     {
      %item = gunItem.getID();
     }
   %player.oldTool = %player.tool[%slot];

   %player.tool[%slot] = %item;
   messageClient(%client,'MsgItemPickup','',%slot,%item);
  }

the %player in your command is never set to anything, and your script expects it to be set -- so its not going to work.

try this:
Code: [Select]
function serverCmdgunswitchtest(%client)
  {
   %player = %client.player;
   %slot = 1;
   if (!isObject(%client.oldTool))
     {
      %item = %player.oldTool;
     }
   else
     {
      %item = gunItem.getID();
     }
   %player.oldTool = %player.tool[%slot];

   %player.tool[%slot] = %item;
   messageClient(%client,'MsgItemPickup','',%slot,%item);
  }

Tried this and all it does it remove my current item when I type it the first time and when I type it a second time, it puts it back.

Code: [Select]
function serverCmdGunSwitchTest(%client)
{
%player = %client.player;
%slot = 1;
%item = gunItem.getID();
%player.tool[%slot] = %item;
messageClient(%client,'MsgItemPickup','',%slot,%item);
}

Code: [Select]
function serverCmdGunSwitchTest(%client)
{
%player = %client.player;
%slot = 1;
%item = gunItem.getID();
%player.tool[%slot] = %item;
messageClient(%client,'MsgItemPickup','',%slot,%item);
}
That works wonderfully, thank you.

Tried this and all it does it remove my current item when I type it the first time and when I type it a second time, it puts it back.
thats what happens when I try to script w/o testing things.

this will switch an item with the gun

Code: [Select]
function serverCmdgunswitchtest(%client)
  {
   %player = %client.player;
   %slot = 1;
   if (isObject(%player.oldTool))
     {
      %item = %player.oldTool;
     }
   else
     {
      %item = gunItem.getID();
     }
   %player.oldTool = %player.tool[%slot];

   %player.tool[%slot] = %item;
   messageClient(%client,'MsgItemPickup','',%slot,%item);
  }