Command Help.
#1

Ok so I've got a command that is supposed to be like this:

COMMAND: /playername [ID]

and its supposed to respond like this:

RESPONSE: ID: [ID] | Player Name: [PLAYERNAME]

This is the code to do it:

pawn Код:
if (!strcmp("/playername", cmdtext, false, 11))
    {
        if(!cmdtext[11])return SendClientMessage(playerid,0xFF0000FF,"USAGE: /playername [ID]");
        {
        new str[128];
        new name;
        name = GetPlayerName(cmdtext[12],str,sizeof(str));
        SendClientMessage(playerid,0x00000000,"ID: "+cmdtext[12]+ " Is Player Name: "+name+".");
        return true;
        }
    }
But i cant figure out how to make it so it shows the Response, i have
pawn Код:
SendClientMessage(playerid,0x00000000,"ID: "+cmdtext[12]+ " Is Player Name: "+name+".");
As the response but i get an error saying:

Код:
error 001: expected token: "-string end-", but found "-identifier-"
I cant figure out how to combine strings with variables i used the plus oppertation but it is not correct. I get the error posted, when i try. How can i solve this issue?
Reply
#2

ui need another " in there
Reply
#3

You will need either sscanf or strtok for that. Search around a bit.
Reply
#4

Use strtok, a lot easier.

pawn Код:
//OnPlayerCommandText
    new cmd[128], idx;
    cmd = strtok(cmdtext, idx);

    if(!strcmp("/playername", cmd)) {
        new tmp[128];
        tmp = strtok(cmdtext, idx);
        if(!IsPlayerConnected(strval(tmp))) return SendClientMessage(playerid, COLOR_RED, "ERROR: Invalid player.");
        new name[MAX_PLAYER_NAME];
        GetPlayerName(strval(tmp), name, sizeof(name));
        new message[128];
        format(message, sizeof(message), "RESPONSE: ID: %i | Player Name: %s", strval(tmp), name);
        SendClientMessage(playerid, COLOR_GREEN, message);
        return 1;
    }
Try that, if you get errors about not having strtok, put this at the bottom of your script:

pawn Код:
strtok(const string[], &index)
{
    new length = strlen(string);
    while ((index < length) && (string[index] <= ' '))
    {
        index++;
    }
 
    new offset = index;
    new result[20];
    while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
    {
        result[index - offset] = string[index];
        index++;
    }
    result[index - offset] = EOS;
    return result;
}
Reply
#5

I think sscanf is easier + faster here is a version of what you want that should work.

pawn Код:
if (!strcmp("/playername", cmdtext, false, 11))
{
    new id;
    if(sscanf(params,"d",id))return SendClientMessage(playerid,0xff0000FF,"USAGE: /playername [ID]");
    {
        if(id != INVALID_PLAER_ID)
        {
            new str[128];
            format(str,128,"ID:%i Is Player Name:%s",id,PlayerName(playerid));
            SendClientMessage(playerid,0xff0000FF,str);
            return true;
        }
        else return SendClientMessage(playerid,0xff0000FF,"ERROR: player not found!");
    }
}
and at bottom of script
pawn Код:
stock PlayerName(playerid)
{
    new pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid,pname,sizeof(pname));
    return pname;
}
Note you will need the sscanf include or sscanf 2.0(plugin) for this method.
Reply
#6

I do not understand how to use sscanf, or strtok can someone breif me on how to use them??
Reply
#7

I couldn't explain that but look for tutorials that use them. might be a good idea to read though the topics.
Strtok: https://sampwiki.blast.hk/wiki/Strtok
sscanf: http://forum.sa-mp.com/showthread.ph...ghlight=sscanf

These are the places i learned from, i beleive its best to use sscanf as its faster.
Reply
#8

i cant get sscanf to work. i followed the plugins directions but it doesnt work

EDIT: ok ill use strtok becuase i cant install the plugin for sscanf.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)