Author Topic: [FIXED] AFK Help  (Read 1018 times)

I tried to make an AFK script, but it says it has a syntax error. Not exactly sure where it is, but I expect it has something to do with MessageAll

Code: [Select]
function ServerCmdAFK(%client)
{
afk = 0;
if(afk == 0)
{
findclientbyname(%client);
MessageAll(",%client SPC "is AFK");
afk = 1;
}
else if(afk == 1)
{
findclientbyname(%client);
MessageAll(",%client SPC "is no longer AFK");
afk = 0;
}
}
« Last Edit: February 13, 2014, 09:12:06 AM by Czar »

I tried to make an AFK script, but it says it has a syntax error. Not exactly sure where it is, but I expect it has something to do with MessageAll

Code: [Select]
function ServerCmdAFK(%client)
{
afk = 0;
if(afk == 0)
{
findclientbyname(%client);
MessageAll(",%client SPC "is AFK");
afk = 1;
}
else if(afk == 1)
{
findclientbyname(%client);
MessageAll(",%client SPC "is no longer AFK");
afk = 0;
}
}
...Not gonna waste my time in describing to you all that you did wrong.
Code: [Select]
function serverCmdAFK(%client)
{
      if(!%client.afk)
      {
            %client.afk = true;
            messageAll('', "\c3" @ %client.name SPC "\c6is now AFK.");
      }

      else if(%client.afk)
      {
            %client.afk = false;
            messageAll('', "\c3" @ %client.name SPC "\c6is no longer AFK.");
      }
}
« Last Edit: February 12, 2014, 09:47:41 AM by jes00 »

...Not gonna waste my time in describing to you all that you did wrong.
Code: [Select]
function serverCmdAFK(%client)
{
      if(!%client.afk)
      {
            %client.afk = true;
            messageAll('', "\c3" @ %client SPC "\c6is now AFK.");
      }

      else if(%client.afk)
      {
            %client.afk = false;
            messageAll('', "\c3" @ %client SPC "\c6is no longer AFK.");
      }
}
Thank you

It seems that it shows my object ID, should I add "findclientbyname(%client)"?

It seems that it shows my object ID, should I add "findclientbyname(%client)"?
Oh woops. Forgot to do %client.name.

FYI. findClientByName returns your object ID. Your supposed to use findClientByName("Whatever") to get %client. Not the other way around.
Modified the code in my original post.

...Not gonna waste my time in describing to you all that you did wrong.

Then you shouldn't waste your time in coding help. Spoonfeeding code is counterproductive. He's trying to learn.

Let me try and explain..

Code: [Select]
afk = 0;
if(afk == 0)
Here is your first mistake. In torquescript, to reference a variable, you must have either a % or a $ before it. $ means that it is a global variable, here you don't want one, so you want % which is a local variable.

Code: [Select]
findclientbyname(%client);This line does nothing. You are calling a function to find a client by the given name. The name you are searching for is something like "25723". Its an object ID, not a name. %client is the client, not the name.
If %client was the name, this line would still do nothing. The function findClientByName returns the client, which you are supposed to set to a variable, something like %client = findClientByName(%name); .

Code: [Select]
MessageAll(",%client SPC "is AFK");This line you almost had right. With messageAll, the first parameter uses apostrophes instead of quotation marks (  '  instead of  "  ). Here you only used one quotation mark, which is a fairly common mistake, you just read it somewhere and rewrote it as it looked.
Again, %client is the object ID, not the name. You want to use %client.name as jes00 stated.

Other than that, you just have logic error. Your code will (or should) set %afk to 0, then check if its 0, which it is, and always will be as it never changes in between. You want to store the variable on the client, so that it isn't lost when the function is finished. Using %client.afk is the easiest way to do this.


Code: [Select]
function ServerCmdAFK(%client)
{
afk = 0;
if(afk == 0)
{
findclientbyname(%client);
MessageAll(",%client SPC "is AFK");
afk = 1;
}
else if(afk == 1)
{
findclientbyname(%client);
MessageAll(",%client SPC "is no longer AFK");
afk = 0;
}
}

Let me try and explain..

Code: [Select]
afk = 0;
if(afk == 0)
Here is your first mistake. In torquescript, to reference a variable, you must have either a % or a $ before it. $ means that it is a global variable, here you don't want one, so you want % which is a local variable.

Code: [Select]
findclientbyname(%client);This line does nothing. You are calling a function to find a client by the given name. The name you are searching for is something like "25723". Its an object ID, not a name. %client is the client, not the name.
If %client was the name, this line would still do nothing. The function findClientByName returns the client, which you are supposed to set to a variable, something like %client = findClientByName(%name); .

Code: [Select]
MessageAll(",%client SPC "is AFK");This line you almost had right. With messageAll, the first parameter uses apostrophes instead of quotation marks (  '  instead of  "  ). Here you only used one quotation mark, which is a fairly common mistake, you just read it somewhere and rewrote it as it looked.
Again, %client is the object ID, not the name. You want to use %client.name as jes00 stated.

Other than that, you just have logic error. Your code will (or should) set %afk to 0, then check if its 0, which it is, and always will be as it never changes in between. You want to store the variable on the client, so that it isn't lost when the function is finished. Using %client.afk is the easiest way to do this.


Code: [Select]
function ServerCmdAFK(%client)
{
afk = 0;
if(afk == 0)
{
findclientbyname(%client);
MessageAll(",%client SPC "is AFK");
afk = 1;
}
else if(afk == 1)
{
findclientbyname(%client);
MessageAll(",%client SPC "is no longer AFK");
afk = 0;
}
}
Thanks for explaining me that, clears everything up for me.

...Not gonna waste my time in describing to you all that you did wrong.

Spoonfeeding like this isn't really much more helpful than just walking into a topic and posting something like this:

function servercmdafk(%a){messageall(0,%a.name@" is "@((%a.afk=!%a.afk)?"":"no longer ")@"AFK");}