SA-MP Forums Archive
Cant set the level. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Cant set the level. (/showthread.php?tid=419192)



Cant set the level. - Audi_Quattrix - 27.02.2013

I type in Example: /setlevel 0 3 but it sets level to 0
pawn Код:
CMD:setlevel(playerid, params[])
{
    new targetid;
    new level;
    new string[128];
    if (IsPlayerAdmin(playerid) || PlayerInfo[playerid][AdminLevel] == 5)
    {
        if (level > 5) return SendClientMessage(playerid,COLOR_RED,"Error:Max level is 5!");
        if (level < 0) return SendClientMessage(playerid,COLOR_RED,"Error:Min level is 0!");
        if (level >= 0 && level <= 5)
        {
            new name[MAX_PLAYER_NAME];
            new PlayerName[MAX_PLAYER_NAME];
            GetPlayerName(playerid, name, sizeof(name));
            GetPlayerName(targetid, PlayerName, sizeof(PlayerName));
            format(string, sizeof(string), "%s Has Given You Admin Level %d .", name, level);
            SendClientMessage(targetid, 0xD8D8D8FF, string);
            format(string, sizeof(string), "You Gave Admin Level %d To %s .", level, PlayerName);
            SendClientMessage(playerid, 0xD8D8D8FF, string);
            PlayerInfo[targetid][AdminLevel] = level;
            return 1;
        }
    }
    return 1;
}



Re: Cant set the level. - Misiur - 27.02.2013

You forgot about sscanf


Re: Cant set the level. - Audi_Quattrix - 27.02.2013

I tryed to add it i had same error so i deleted it thinking it isnt sscanf...


Re: Cant set the level. - Jeffry - 27.02.2013

pawn Код:
CMD:setlevel(playerid, params[])
{
    new string[128];
    if (IsPlayerAdmin(playerid) || PlayerInfo[playerid][AdminLevel] == 5)
    {
        new tmp[20], tmp2[20], idx;
        tmp = strtok(params, idx); tmp2 = strtok(params, idx);
        if(!strlen(tmp) || strlen(tmp2))  return SendClientMessage(playerid,COLOR_RED,"Usage: /setlevel [playerid] [level]!");
        new targetid = strval(tmp);
        new level = strval(tmp2);
        if (level > 5) return SendClientMessage(playerid,COLOR_RED,"Error:Max level is 5!");
        if (level < 0) return SendClientMessage(playerid,COLOR_RED,"Error:Min level is 0!");
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid,COLOR_RED,"Error:Player not connected!");
        new name[MAX_PLAYER_NAME];
        new PlayerName[MAX_PLAYER_NAME];
        GetPlayerName(playerid, name, sizeof(name));
        GetPlayerName(targetid, PlayerName, sizeof(PlayerName));
        format(string, sizeof(string), "%s Has Given You Admin Level %d .", name, level);
        SendClientMessage(targetid, 0xD8D8D8FF, string);
        format(string, sizeof(string), "You Gave Admin Level %d To %s .", level, PlayerName);
        SendClientMessage(playerid, 0xD8D8D8FF, string);
        PlayerInfo[targetid][AdminLevel] = level;
        return 1;
    }
    return 1;
}

//from https://sampwiki.blast.hk/wiki/Strtok
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;
}
Some might say use sscanf, that's up to you. I made it with strtok now, because I prefer it.


Re: Cant set the level. - 2KY - 27.02.2013

Quote:
Originally Posted by Jeffry
Посмотреть сообщение
Some might say use sscanf, that's up to you. I made it with strtok now, because I prefer it.
Yikes!

pawn Код:
CMD:setlevel ( playerid, params[] )
{
    new
        user,
        level;
       
    if( !IsPlayerAdmin ( playerid ) || PlayerInfo [ playerid ] [ AdminLevel ] < 5)
        return false; // Invalid permissions!
       
    if( sscanf( params, "ui", user, level ) )
        return SendClientMessage(playerid,COLOR_RED,"Usage: /setlevel [playerid] [level]!");
       
    PlayerInfo [ user ] [ AdminLevel ] = level;
   
    new
        name [ MAX_PLAYER_NAME ],
        PlayerName [ MAX_PLAYER_NAME ];
       
    GetPlayerName ( playerid, name, sizeof ( name ) );
    GetPlayerName ( user, PlayerName, sizeof ( PlayerName ) );
       
    format ( string, sizeof ( string ), "%s Has Given You Admin Level %d .", name, level );
    SendClientMessage ( user, 0xD8D8D8FF, string );
       
    format ( string, sizeof ( string ), "You Gave Admin Level %d To %s .", level, PlayerName );
    SendClientMessage( playerid, 0xD8D8D8FF, string );
    return true;
}
With sscanf.