SA-MP Forums Archive
GetDistanceBetweenPlayers Not working properly - 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: GetDistanceBetweenPlayers Not working properly (/showthread.php?tid=417030)



GetDistanceBetweenPlayers Not working properly - Patrick - 19.02.2013

hello guys. im trying to test with my friend Jari_Johnson, when i type or him /rob (ID) i am near him its saying
this error
pawn Код:
if(GetDistanceBetweenPlayers(playerid,ID) > 5)
    {
        format(string,sizeof(string),"%s(%d) is too far away. You cannot reach him to rob him.",PlayerName(ID),ID);
        SendClientMessage(playerid,COLOR_RED_ERROR,string);
        return 1;
    }
this is the whole command
pawn Код:
CMD:rob(playerid,params[])
{
    new string[128];
    new ID;
    if(sscanf(params, "u", ID))
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"Usage: /rob (Player Name/ID)");
        return 1;
    }
    if(IsCuffed[playerid] == 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cannot use this command while your cuffed");
        return 1;
    }
    if(JailTime[playerid] >= 1)
    {
        SendClientMessage(playerid, COLOR_RED_ERROR, "You are in jail! you cant rob someone");
        return 1;
    }
    if(IsSpawned[playerid] != 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You must be alive and spawned in order to be able to use this command.");
        return 1;
    }
    if(IsFrozen[playerid] == 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You have been frozen by a Server Administrator. You cannot use this command.");
        return 1;
    }
    if(InAdminMode[ID] == 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cannot use this command on this player because they are in Administrator mode.");
        return 1;
    }
    if(IsKidnapped[playerid] == 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You are kidnapped. You cannot use this command.");
        return 1;
    }
    if(gTeam[playerid] == TEAM_POLICE || gTeam[playerid] == TEAM_ARMY || gTeam[playerid] == TEAM_CIA)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cannot rob players with your class/skill.");
        return 1;
    }
    if(!IsPlayerConnected(ID))
    {
        format(string,sizeof(string),"The player ID (%d) is not connected to the server. You cannot rob them",ID);
        SendClientMessage(playerid,COLOR_RED_ERROR,string);
        return 1;
    }
    if(GetDistanceBetweenPlayers(playerid,ID) > 5)
    {
        format(string,sizeof(string),"%s(%d) is too far away. You cannot reach him to rob him.",PlayerName(ID),ID);
        SendClientMessage(playerid,COLOR_RED_ERROR,string);
        return 1;
    }
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER || GetPlayerState(playerid) == PLAYER_STATE_PASSENGER)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cannot rob someone while in a vehicle. Exit the vehicle first.");
        return 1;
    }
    if(GetPlayerState(ID) == PLAYER_STATE_DRIVER || GetPlayerState(ID) == PLAYER_STATE_PASSENGER)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cannot rob someone while they are in a vehicle. Get them to exit the vehicle first.");
        return 1;
    }
    if(playerid == ID)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cannot rob yourself, how can you even manage that?");
        return 1;
    }
    if(IsSpawned[ID] != 1)
    {
        format(string,sizeof(string),"%s(%d) is not spawned. You cannot rob dead people ..",PlayerName(ID),ID);
        SendClientMessage(playerid,COLOR_RED_ERROR,string);
        return 1;
    }
    if(IsFrozen[ID] == 1)
    {
        format(string,sizeof(string),"%s(%d) is frozen by a Server Administrator. You cannot rob them.",PlayerName(ID),ID);
        SendClientMessage(playerid,COLOR_RED_ERROR,string);
        return 1;
    }
    if(AttemptedToRobRecently[playerid] >= 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You are too tired from your last rob attempt. Please wait before robbing again.");
        return 1;
    }
    if(HasRobbedRecently[playerid] >= 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You are too tired from the last person you robbed. Please wait before robbing again.");
        return 1;
    }
    if(GetPlayerMoney(ID) <= 0)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"That player has no money in their pockets. What would be the point in robbing them?");
        return 1;
    }
    new crand = random(100);
    if(crand <= 30)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"Rob attempt failed. The player slipped out of your grasp.");
        AttemptedToRobRecently[playerid] =35;
        return 1;
    }
    if(HasSecureWallet[ID] > 1)
    {
        SendClientMessage(playerid,COLOR_RED_ERROR,"You cant rob this person. He have security wallet.");
        HasSecureWallet[ID] = HasSecureWallet[ID] - 1;
        format(string, sizeof(string), "%s has attempted to rob you, you now have %d security wallet left.", PlayerName(playerid),HasSecureWallet[ID]);
        SendClientMessage(ID, -1, string);
        HasRobbedRecently[playerid] =180;
        return 1;
    }
    if(GetDistanceBetweenPlayers(playerid,ID) <= 5 && crand > 30)
    {
        new current_zone = player_zone[playerid];
        new mrand =random(GetPlayerMoney(ID));
        SendClientMessage(playerid,COLOR_GREY,"[[_Player Robbed_]]");
        format(string,sizeof(string),"You have robbed $%d from %s(%d). Careful they dont come after you!",mrand,PlayerName(ID),ID);
        SendClientMessage(playerid,COLOR_RED,string);
        GivePlayerMoney(playerid,mrand);
        IncreaseWantedLevel(playerid,8);
        HasRobbedRecently[playerid] =180;
        IncreasePlayerScore(playerid,3);

        SendClientMessage(ID,COLOR_GREY,"[[_Robbed_]]");
        format(string,sizeof(string),"You have had $%d robbed from you by %s(%d). Kill them for your money back or run!",mrand,PlayerName(playerid),playerid);
        SendClientMessage(ID,COLOR_RED,string);
        GivePlayerMoney(ID,-mrand);

        format(string,sizeof(string),"[DISPATCH] Robbery: %s(%d) has robbed $%d from %s(%d). Location: %s.",PlayerName(playerid),playerid,mrand,PlayerName(ID),ID,zones[current_zone][zone_name]);
        SendClientMessageToAllCops(string);
        return 1;
    }
    return 1;
}
why doesn't it work properly?

this the GetDistanceBetweenPlayers function
pawn Код:
forward Float:GetDistanceBetweenPlayers(p1,p2);
public Float:GetDistanceBetweenPlayers(p1,p2)
{
    new /*Float:x1,Float:y1,Float:z1,*/Float:x2,Float:y2,Float:z2;
    /*if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));*/

    //GetPlayerPos(p1, x1, y1, z1);
    GetPlayerPos(p2, x2, y2, z2);
    return GetPlayerDistanceFromPoint(p1, x2, y2, z2);
}



Re: GetDistanceBetweenPlayers Not working properly - Roko_foko - 19.02.2013

I don't see any mistake here.
Suggestion: Why don't you try with IsPlayerInRangeOfPoint? If same problem occurs, the problem is probably in fscanf.

Hope this helps. (:


Re: GetDistanceBetweenPlayers Not working properly - Patrick - 19.02.2013

@Roko_foko ye, i dont see any mistakes too. i dont know why that error occurs. can you show me an example you mean?


Re: GetDistanceBetweenPlayers Not working properly - Roko_foko - 19.02.2013

Quote:
Originally Posted by pds2012
Посмотреть сообщение
@Roko_foko ye, i dont see any mistakes too. i dont know why that error occurs. can you show me an example you mean?
Sure, take a look.
pawn Код:
//...
if(!IsPlayerConnected(ID))
{
    format(string,sizeof(string),"The player ID (%d) is not connected to the server. You cannot rob them",ID);
    SendClientMessage(playerid,COLOR_RED_ERROR,string);
    return 1;
}
new Float:A[3];
GetPlayerPos(ID,A[0],A[1],A[2]);
if(IsPlayerInRangeOfPoint(playerid,5.0,A[0],A[1],A[2]))
{
    format(string,sizeof(string),"%s(%d) is too far away. You cannot reach him to rob him.",PlayerName(ID),ID);
    SendClientMessage(playerid,COLOR_RED_ERROR,string);
    return 1;
}



Re: GetDistanceBetweenPlayers Not working properly - RajatPawar - 19.02.2013

pawn Код:
forward GetDistanceBetweenPlayers(p1, p2);
public GetDistanceBetweenPlayers(p1, p2)
{
    new Float:x2,Float:y2,Float:z2; new Float:x1,Float:y1,Float:z1;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return GetPlayerDistanceFromPoint(p1, x2, y2, z2);
}
The best way to debug is to use a make do command like /showdistance to verify if the function works.



Re: GetDistanceBetweenPlayers Not working properly - Patrick - 19.02.2013

@Rajat_Pawar, its giving me Warning: warning 213: tag mismatch


Re: GetDistanceBetweenPlayers Not working properly - RajatPawar - 19.02.2013

pawn Код:
forward GetDistanceBetweenPlayers(p1, p2);
public GetDistanceBetweenPlayers(p1, p2)
{
    new Float:x2,Float:y2,Float:z2; new Float:x1,Float:y1,Float:z1;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    new Float:distance = GetPlayerDistanceFromPoint(p1, x2, y2, z2);
    return distance;
}



Re: GetDistanceBetweenPlayers Not working properly - Patrick - 19.02.2013

Still same Warning: warning 213: tag mismatch

on this line

pawn Код:
forward GetDistanceBetweenPlayers(p1, p2);
public GetDistanceBetweenPlayers(p1, p2)
{
    new Float:x2,Float:y2,Float:z2; new Float:x1,Float:y1,Float:z1;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;//Warning line
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    new Float:distance = GetPlayerDistanceFromPoint(p1, x2, y2, z2);
    return distance;//Warning line
}



Re: GetDistanceBetweenPlayers Not working properly - RajatPawar - 19.02.2013

pawn Код:
forward GetDistanceBetweenPlayers(p1, p2);
public GetDistanceBetweenPlayers(p1, p2)
{
    new Float:x2,Float:y2,Float:z2; new Float:x1,Float:y1,Float:z1;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return 0;
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    new Float:distance = GetPlayerDistanceFromPoint(p1, x2, y2, z2);
    return distance;
}
Just made a few changes, I can't really figure out what else is wrong!


Re: GetDistanceBetweenPlayers Not working properly - Roko_foko - 19.02.2013

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
pawn Код:
forward GetDistanceBetweenPlayers(p1, p2);
public GetDistanceBetweenPlayers(p1, p2)
{
    new Float:x2,Float:y2,Float:z2; new Float:x1,Float:y1,Float:z1;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return 0;
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    new Float:distance = GetPlayerDistanceFromPoint(p1, x2, y2, z2);
    return distance;
}
Just made a few changes, I can't really figure out what else is wrong!
Do you really need x1,y1,z1? No.
You are returning distance which is Float, so the function needs to be Float.
So this function should look like this:
pawn Код:
forward Float:GetDistanceBetweenPlayers(p1, p2);
public Float:GetDistanceBetweenPlayers(p1, p2)
{
    new Float:x2,Float:y2,Float:z2;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return -1.0;
    GetPlayerPos(p2,x2,y2,z2);
    new Float:distance = GetPlayerDistanceFromPoint(p1, x2, y2, z2);
    return distance;
}