Sombody help me in this code please!
#1

I tried to make a command returns 3 things but it didn't success as well!

The command is /ddealer to request a drug dealer!
1. No Drug Dealers are online (If the playerid typed the command but no ddealers found)
2. Drug Dealers Far Away (If the playerid typed the command while he is far away from drug dealers)
3. Drug Dealers Near playerid (It explains itself)

Here is a sample of the command, please somebody tell me how to fix all of these mess:
pawn Код:
CMD:ddealer(playerid, params[])
{
    new string[128], targetid;
    // no drug dealers online
    if(PlayerInfo[playerid][pSkill] == SKILL_DRUGDEALER) return SendClientMessage(playerid, COLOR_RED, "You are already a drug dealer, you cannot use this command.");

    targetid = GetClosestDDealer(playerid, 7.0);
    if(targetid == INVALID_PLAYER_ID) return SendClientMessage(playerid,COLOR_RED, "Sorry! There are no drug dealers online at the moment.");
    if(targetid == ??) // when there are drug dealers online found but they are away from the playerid
    {
        SendClientMessage(playerid, COLOR_WHITE, "You have {33CCFF}Requested {FFFFFF}a {C73041}Drug Dealer {FFFFFF}to your current location!");
        format(string, sizeof(string), "%s(%d) {FFFFFF}has {33CCFF}Requested {FFFFFF}a {C73041}Drug Dealer {FFFFFF}at {B3B3B3}%s", GetName(playerid), playerid, GetPlayerLocation(playerid));
        SendClientMessage(targetid, COLOR_COPS, string);
        return 1;
    }
    //else if there a drug dealer near the playerid
    ShowDDealerTextDraws(targetid, playerid);

    format(string, sizeof(string), "%s(%d) {FFFFFF}is now taking a look on your list.", GetName(playerid), playerid);
    SendClientMessage(targetid, COLOR_YELLOW, string);

    format(string, sizeof(string), "You are now viewing {0040FF}%s(%i)'s {FFFFFF}list.", GetName(targetid), targetid);
    SendClientMessage(playerid, COLOR_WHITE, string);
    return 1;
}

stock GetClosestDDealer(playerid, Float:dis)
{
    new Float:dis2, player = INVALID_PLAYER_ID;
    for(new x = 0; x < MAX_PLAYERS; x++)
    {
        if(!IsPlayerConnected(x)) continue;
        if(GetPlayerState(x) == PLAYER_STATE_SPECTATING) continue;
        if(x == playerid) continue;
        if(PlayerTeam[x] != TEAM_CIV) continue;
        if(PlayerInfo[x][pSkill] != SKILL_DRUGDEALER) continue;
        dis2 = GetDistanceBetweenPlayers(x,playerid);
        if(dis2 < dis && dis2 != -1.00)
        {
            dis = dis2;
            player = x;
        }
    }
    return player;
}
Edit: Everything fixed now but if i remove the loop from the code below.
When adding it: it always return the message even if there are drug dealers online.
Without it: Command works fine for the "Near" and "Far Away"!
pawn Код:
CMD:ddealer(playerid, params[])
{
    new string[128], Float:x,Float:y,Float:z;
    new targetid = GetClosestDDealer(playerid, 7.0);
    GetPlayerPos(targetid,x,y,z);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerInfo[i][pSkill] != SKILL_DRUGDEALER) return SendClientMessage(playerid,COLOR_RED, "Sorry! There are no drug dealers online at the moment.");
    }
    if(IsPlayerInRangeOfPoint(playerid,7,x,y,z))
    {
        ShowDDealerTextDraws(targetid, playerid);

        format(string, sizeof(string), "%s(%d) {FFFFFF}is now taking a look on your list.", GetName(playerid), playerid);
        SendClientMessage(targetid, COLOR_YELLOW, string);

        format(string, sizeof(string), "You are now viewing {0040FF}%s(%i)'s {FFFFFF}list.", GetName(targetid), targetid);
        SendClientMessage(playerid, COLOR_WHITE, string);
        return 1;
    }
    SendClientMessage(playerid, COLOR_WHITE, "You have {33CCFF}Requested {FFFFFF}a {C73041}Drug Dealer {FFFFFF}to your current location!");
    format(string, sizeof(string), "%s(%d) {FFFFFF}has {33CCFF}Requested {FFFFFF}a {C73041}Drug Dealer {FFFFFF}at {B3B3B3}%s", GetName(playerid), playerid, GetPlayerLocation(playerid));
    SendClientMessage(targetid, COLOR_COPS, string);
    return 1;
}
Reply
#2

You shouldn't be using return inside a loop because it will end the loop at the first player that isn't a drug dealer so if there is players after him that are drug dealers it will never get to them.

Try something like this:

pawn Код:
new dealerCount = 0;
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(PlayerInfo[i][pSkill] == SKILL_DRUGDEALER)
        dealerCount++;
}
if(dealerCount == 0) return SendClientMessage(playerid,COLOR_RED, "Sorry! There are no drug dealers online at the moment.");
Reply
#3

Quote:
Originally Posted by Chenko
Посмотреть сообщение
You shouldn't be using return inside a loop because it will end the loop at the first player that isn't a drug dealer so if there is players after him that are drug dealers it will never get to them.

Try something like this:

pawn Код:
new dealerCount = 0;
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(PlayerInfo[i][pSkill] == SKILL_DRUGDEALER)
        dealerCount++;
}
if(dealerCount == 0) return SendClientMessage(playerid,COLOR_RED, "Sorry! There are no drug dealers online at the moment.");
This will work.
Reply
#4

Quote:
Originally Posted by Dubya
Посмотреть сообщение
This will work.
Wow, thank you for your useless contribution.

pawn Код:
CMD:ddealer(playerid, params[])
{
    if(PlayerInfo[playerid][pSkill] == SKILL_DRUGDEALER) return SendClientMessage(playerid, COLOR_RED, "You are already a drug dealer, you cannot use this command.");
    new targetid = INVALID_PLAYER_ID, Float:range = -1.0, count = 0, Float:x, Float:y, Float:z;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i)) continue;
        if(i == playerid) continue;
        if(GetPlayerState(i) == PLAYER_STATE_SPECTATING) continue;
        if(PlayerTeam[i] != TEAM_CIV) continue;
        if(PlayerInfo[i][pSkill] != SKILL_DRUGDEALER) continue;
        count++;
        GetPlayerPos(i, x, y, z);
        new Float:newrange = GetPlayerDistanceFromPoint(playerid, x, y, z);
        if((newrange > range) && (range != -1.0)) continue;
        targetid = i;
        range = newrange;
    }
    if(!count) return SendClientMessage(playerid,COLOR_RED, "Sorry! There are no drug dealers online at the moment.");
    new string[145];
    if(GetPlayerDistanceFromPoint(playerid, x, y, z) > 7.0)
    {
        SendClientMessage(playerid, COLOR_WHITE, "You have {33CCFF}Requested {FFFFFF}a {C73041}Drug Dealer {FFFFFF}to your current location!");
        format(string, sizeof(string), "%s(%d) {FFFFFF}has {33CCFF}Requested {FFFFFF}a {C73041}Drug Dealer {FFFFFF}at {B3B3B3}%s", GetName(playerid), playerid, GetPlayerLocation(playerid));
        SendClientMessage(targetid, COLOR_COPS, string);
    }
    else
    {
        ShowDDealerTextDraws(targetid, playerid);
        format(string, sizeof(string), "%s(%d) {FFFFFF}is now taking a look on your list.", GetName(playerid), playerid);
        SendClientMessage(targetid, COLOR_YELLOW, string);
        format(string, sizeof(string), "You are now viewing {0040FF}%s(%i)'s {FFFFFF}list.", GetName(targetid), targetid);
        SendClientMessage(playerid, COLOR_WHITE, string);
    }
    return 1;
}
Reply
#5

Worked!! Thanks all..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)