SendClientMessage problem!
#1

I've tried alot of GetClosestPlayer functions and all causes the same problem in SendClientMessage. I really don't know what exactly causes the problem, the GetClosestPlayer function or my arranging of the Scripting Lines.

Look At This:

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if ((newkeys & KEY_LOOK_BEHIND) && !(oldkeys & KEY_LOOK_BEHIND))
    {
        if(PlayerTeam[playerid] == TEAM_CIV && AdminDuty[playerid] == false)
        {
            if(PlayerInfo[playerid][pSkill] == 1)
            {
                new targetid = GetClosestPlayer(playerid, 7.0);
                if(IsKidnapped[playerid] == 1 && Spawned[playerid] == 0) return 1;
                if(targetid == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_WHITE, "{FF0000}[Error:] {FFFFFF}There are no players close enough to rape.");
                //if(GetPlayerWantedLevel(targetid) == 0) return SendClientMessage(playerid, COLOR_WHITE, "{FF0000}[Error:] {FFFFFF}There are no players close enough to rape.");
                if(Spawned[targetid] == 0 || Freezed[targetid] == 1) return SendClientMessage(playerid, COLOR_WHITE, "{FF0000}[Error:] {FFFFFF}There are no players close enough to rape.");
                if(ArrestCuffed[targetid] == 1) return SendClientMessage(playerid, COLOR_RED, "You cannot rape cuffed players.");
            }
        }
    }
    return 1;
}
The Error Message "There are no players close enough to rape." only appears if i make the //Comment Line compilable (uncommented), and doesn't appear if i make it as comment!
How to make the error message appears ? cuz i want to delete all the comment line.

GetClosestPlayer function: (originally posted by nmader)
pawn Код:
stock 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));
}

stock GetClosestPlayer(playerid, Float:dis)
{
    new x, Float:dis2, player;
    player = -1;
    for (x = 0; x < MAX_PLAYERS; x++)
    {
        if(IsPlayerConnected(x) && x != playerid && GetPlayerState(x) != PLAYER_STATE_SPECTATING)
        {
            dis2 = GetDistanceBetweenPlayers(x,playerid);
            if(dis2 < dis && dis2 != -1.00)
            {
                dis = dis2;
                player = x;
            }
        }
    }
    return player;
}
Reply
#2

Are you sure that you use those 2 together? If there isn't any player closer, GetClosestPlayer returns -1 and not INVALID_PLAYER_ID (65535) that you check later.
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Are you sure that you use those 2 together? If there isn't any player closer, GetClosestPlayer returns -1 and not INVALID_PLAYER_ID (65535) that you check later.
What do u mean by "2 together" ?

And i tried the problem in 2 cases:
1. Only me in the server.
2. More than 1 player in the server.

#Same_Problem!
Reply
#4

Quote:
Originally Posted by Juvanii
Посмотреть сообщение
What do u mean by "2 together" ?

And i tried the problem in 2 cases:
1. Only me in the server.
2. More than 1 player in the server.

#Same_Problem!
why not use break?

pawn Код:
stock GetClosestPlayer(playerid,Float:limit)
{
    new Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2;
    GetPlayerPos(playerid,x1,y1,z1);
    new Float:Range = 999.9;
    new id = -1;
    foreach(Player,i)
    {
        if(playerid != i)
        {
            GetPlayerPos(i,x2,y2,z2);
            new Float:Dist = GetDistanceBetweenPoints(x1,y1,z1,x2,y2,z2);
            if(floatcmp(Range,Dist) == 1 && floatcmp(limit,Range) == 1)
            {
                Range = Dist;
                id = i;
                break;
            }
        }
    }
    return id;
}
PD: sorry if this is not your question, my English is not very good but I attempt
Reply
#5

@Zume-zero : Because it will not get the "closest" player from the playerid, it will just return the lowest playerid in the radius.
Reply
#6

pawn Код:
stock Float:GetDistanceBetweenPlayers(p1, p2)
{
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return -1.00;
    new Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2;
    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));
}

stock GetClosestPlayer(playerid, Float:dis)
{
    new Float:dis2, player = INVALID_PLAYER_ID;
    for(new x = 0; x < MAX_PLAYERS; x++)
    {
        if(!IsPlayerConnected(x)) continue;
        if(x == playerid) continue;
        if(GetPlayerState(x) == PLAYER_STATE_SPECTATING) continue;
        dis2 = GetDistanceBetweenPlayers(x,playerid);
        if(dis2 < dis && dis2 != -1.00)
        {
            dis = dis2;
            player = x;
        }
    }
    return player;
}
The only real issue I can see is 'targetid' is returning -1 rather than INVALID_PLAYER_ID as Konstantinos stated. Another alternative would to just change 'if(targetid == INVALID_PLAYER_ID)' to 'if(targetid == -1)', but it's more readable and understandable with the first method.
Reply
#7

Without testing it, this should work! Damn, how didn't i see this mistake o.O"
btw rep+ bro.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)