Problem With /rob Command [+REP]
#1

So I Made /rob command to the nearest player but the problem is the player who have the id 0 when he uses it,it says: You cannot rob yourself!

Here is the codes:
pawn Код:
COMMAND:rob(playerid, params[])
    {
        new pname[MAX_PLAYER_NAME];
        GetPlayerName(playerid, pname, sizeof(pname));
        if(!strlen(params))
        {
            GetClosestPlayerToPlayer(playerid);
        }
        else
        {
            ID = strval(params);
            if(robtime[playerid] == 0)
            {
                if(ID != playerid)
                {
                    if(IsPlayerConnected(ID))
                    {
                        new oname[MAX_PLAYER_NAME];
                        GetPlayerName(ID, oname, sizeof(oname));
                        if(GetDistanceBetweenPlayers(playerid, ID) <= DISTANCE_BETWEEN_PLAYERS)
                        {
                            if(!GetPlayerVehicleID(ID) || GetPlayerVehicleID(ID) == GetPlayerVehicleID(ID))
                            {
                                if(GetPlayerMoney(ID) > 0)
                                {
                                    new rob = random(11);
                                    if(rob == 1 || rob == 2 || rob == 10 || rob == 11)
                                    {
                                        new string[256];
                                        format(string, sizeof(string), "%s (%i) noticed you trying to rob him. Attempt failed!",oname, ID);
                                        SendClientMessage(playerid, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~%s Noticed you trying to rob him.~n~Attempt failed!.",oname, ID);
                                        GameTextForPlayer(playerid, string, 5000, 4);
                                        GetPlayerName(playerid,pname,sizeof(pname));
                                        format(string, sizeof(string), "You noticed %s (%i) trying to rob you. His attempt has failed!", pname, playerid);
                                        SendClientMessage(ID, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~You noticed %s trying to rob you.~n~His attempt has failed!", pname, playerid);
                                        GameTextForPlayer(ID, string, 5000, 4);
                                    }
                                    else if(rob == 3)
                                    {
                                        new pcash = GetPlayerMoney(ID);
                                        new robcash = random(pcash);
                                        GivePlayerMoney(ID, -robcash);
                                        GivePlayerMoney(playerid, robcash);
                                        GetPlayerName(playerid, pname, sizeof(pname));
                                        new string[256];
                                        format(string, sizeof(string), "You have robbed $%i from %s (%i).", robcash, oname, ID);
                                        SendClientMessage(playerid, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~Robbed $%i from %s.", robcash, oname);
                                        GameTextForPlayer(playerid, string, 5000, 4);
                                        format(string, sizeof(string), "%s (%i) has robbed $%i from you.", pname, playerid, robcash);
                                        SendClientMessage(ID, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~%s has robbed $%i from you.", pname, robcash);
                                        GameTextForPlayer(ID, string, 5000, 4);
                                    }
                                    else if(rob == 4 || rob == 5 || rob == 8)
                                    {
                                        new pcash = GetPlayerMoney(ID);
                                        new robcash = random(pcash);
                                        new robcash2 = robcash-random(robcash);
                                        GivePlayerMoney(ID, -robcash2);
                                        GivePlayerMoney(playerid, robcash2);
                                        new string[256];
                                        GetPlayerName(playerid, pname, sizeof(pname));
                                        format(string, sizeof(string), "You have robbed $%i from %s (%i).", robcash2, oname, ID);
                                        SendClientMessage(playerid, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~Robbed $%i from %s.", robcash2, oname);
                                        GameTextForPlayer(playerid, string, 5000, 4);
                                        format(string, sizeof(string), "%s (%i) has robbed $%i from you.", pname, playerid, robcash2);
                                        SendClientMessage(ID, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~%s has robbed $%i from you.", pname, robcash2);
                                        GameTextForPlayer(ID, string, 5000, 4);
                                    }
                                    else if(rob == 6 || rob == 7)
                                    {
                                        new pcash = GetPlayerMoney(ID);
                                        new robcash = random(pcash);
                                        new robcash2 = robcash-random(robcash);
                                        new robcash3 = robcash2-random(robcash2);
                                        GivePlayerMoney(ID, -robcash3);
                                        GivePlayerMoney(playerid, robcash3);
                                        new string[256];
                                        GetPlayerName(playerid, pname, sizeof(pname));
                                        format(string, sizeof(string), "You have robbed $%i from %s (%i).", robcash3, oname, ID);
                                        SendClientMessage(playerid, COLOR_ROB, string);
                                        format(string, sizeof(string), "%s (%i) has robbed $%i from you.", pname, playerid, robcash3);
                                        SendClientMessage(ID, COLOR_ROB, string);
                                        format(string, sizeof(string), "~w~Robbed $%i from %s.", robcash3, oname);
                                        GameTextForPlayer(playerid, string, 5000, 4);
                                        format(string, sizeof(string), "~w~%s has robbed $%i from you.", pname, robcash3);
                                        GameTextForPlayer(ID, string, 5000, 4);
                                    }
                                    else
                                    {
                                        SetPlayerHealth(playerid, -69);
                                        new string[256];
                                        GetPlayerName(playerid, pname, sizeof(pname));
                                        format(string, sizeof(string), "Your hand has stuck to %s (%i)'s pocket. ", oname, ID);
                                        SendClientMessage(playerid, COLOR_FAIL, string);
                                        SendClientMessage(playerid, COLOR_FAIL, "He noticed it and ripped your arms off.");
                                        format(string, sizeof(string), "%s (%i)'s hand has stuck to your pocket while trying to rob you.", pname, playerid);
                                        SendClientMessage(ID, COLOR_FAIL, string);
                                        SendClientMessage(ID, COLOR_FAIL, "You noticed it and ripped his arms off.");
                                        format(string, sizeof(string), "*** %s (%i) has bled to death.", pname, playerid);
                                        SendClientMessageToAll(0x880000FF, string);
                                        format(string, sizeof(string), "~w~%s has ripped your arms off.", oname, ID);
                                        GameTextForPlayer(playerid, string, 5000, 4);
                                        format(string, sizeof(string), "~w~Ripped %s's arms off.", oname, ID);
                                        GameTextForPlayer(ID, string, 5000, 4);
                                    }
                                    robtime[playerid] = 1;
                                    SetTimerEx("robtimer", ROB_TIME, false, "i", playerid);
                                }
                                else
                                {
                                    new string[256];
                                    format(string, sizeof(string), "%s (%i) has no money to rob!", oname, ID);
                                    SendClientMessage(playerid, COLOR_ERROR, string);
                                }
                            }
                            else
                            {
                                new string[256];
                                format(string, sizeof(string), "%s (%i) has to be in the same vehicle as you to be able to rob!", oname, ID);
                                SendClientMessage(playerid, COLOR_ERROR, string);
                            }
                        }
                        else
                        {
                            new string[256];
                            format(string, sizeof(string), "%s (%i) is not close enough to rob.", oname, ID);
                            SendClientMessage(playerid, COLOR_ERROR, string);
                        }
                    }
                    else
                    {
                        SendClientMessage(playerid, COLOR_ERROR, "That player is not connected!");
                    }
                }
                else
                {
                    SendClientMessage(playerid, COLOR_ERROR, "You cannot rob yourself!");
                }
            }
            else
            {
                SendClientMessage(playerid, COLOR_ERROR, "Please wait before robbing someone again.");
            }
        }
        return 1;
}
Reply
#2

If the player has no-one near of him (or if your function isn't well coded), it will most likely return 0, and that's the problem in my opinion.
Reply
#3

Well,i tried that while i was near a player and kept saying: You cannot rob yourself.

EDIT: GetClosestPlayerToPlayer Function Code and there is a Forward for it:
pawn Код:
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 1000.0;
    new targetid = INVALID_PLAYER_ID;
    new Float:x1,Float:y1,Float:z1;
    new Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(i == playerid) continue;
        GetPlayerPos(i,x2,y2,z2);
        tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
        if(tmpdis < dist)
        {
            dist = tmpdis;
            targetid = i;
        }
    }
    return targetid;
}
Reply
#4

Have you, at least, explored the second possibility that your function isn't well coded ?
Reply
#5

well,i got ROB command from a filterscript and Getclosestplayertoplayer function from a post
Reply
#6

Can you show me the code of the function ?
Reply
#7

here:
pawn Код:
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 1000.0;
    new targetid = INVALID_PLAYER_ID;
    new Float:x1,Float:y1,Float:z1;
    new Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(i == playerid) continue;
        GetPlayerPos(i,x2,y2,z2);
        tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
        if(tmpdis < dist)
        {
            dist = tmpdis;
            targetid = i;
        }
    }
    return targetid;
}
Reply
#8

PHP код:

stock Float
:GetDistanceBetweenPoints(Float:x1Float:y1Float:z1Float:x2Float:y2Float:z2)
{
    return 
VectorSize(x1-x2y1-y2z1-z2);
}

stock GetClosestPlayerToPlayer(playeridFloat:dist 10.0)
{
    new 
Float:pos[6];
    
GetPlayerPos(playeridpos[0], pos[1], pos[2]);
    foreach(new 
Player)
    {
        if(
== playerid) continue;
        
GetPlayerPos(ipos[3], pos[4], pos[5]);
        if(
GetDistanceBetweenPoints(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]) <= dist) return i;
    }

This one should work properly.
Reply
#9

pawn Код:
error 017: undefined symbol "VectorSize"
Reply
#10

You must have SAMP 0.3z include to use this code.

As it's written in the wiki page of the function, https://sampwiki.blast.hk/wiki/VectorSize

"This function was added in SA-MP 0.3z and will not work in earlier versions!".
By the way, the function "GetDistanceBetweenPoints" 's credits go to Vince.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)