Chatting with ClientMessages.
#1

Hello, everyone.

Basically, the script should send a ClientMessage to everyone in the radius of the player who "spoke" (so-to-speak). However, no messages pop up. I couldn't try this with multiple people, as I haven't port forwarded, and I haven't multiple computers. I do this so I have a little more control of how the message is styled.

No syntax errors or warnings.

P.S. - I haven't an idea if there is a method to see how many players are connected in total, so I have a variable which iterates up, or down, when a user [(dis)connects].


pawn Код:
#include <a_samp>

new players = 0;

public OnPlayerConnect(playerid) {
    players++;
}

public OnPlayerDisconnect(playerid) {
    players--;
}

public OnPlayerText(playerid, text[]) {
    new name[256], message[256];
    new Float:x, Float:y, Float:z;

    GetPlayerName(playerid, name, sizeof(name));
    format(message, sizeof(message), "%s says, \"%s\"",  name, text);
   
    for (new id = 0; id >= players; id++) {
        if (IsPlayerConnected(id) && IsPlayerInRangeOfPoint(id, 20.0, x, y, z)) {
            SendClientMessage(id, -1, message);
        }
    }
   
    return 0;
}
Reply
#2

Your code wouldn't work because if there was 3 people:
ID 0
ID 1
ID 2
Lets say ID 1 quit then the player variable would change to 2 but it would only reach ID 0 because ID 2 is out of the loop. You could either use foreach or the following:

pawn Код:
public OnPlayerText(playerid, text[])
{
    new name[24], message[128];
    new Float:x, Float:y, Float:z;
    GetPlayerName(playerid, name, sizeof(name));
    format(message, sizeof(message), "%s says, \"%s\"",  name, text);
    GetPlayerPos(playerid, x, y, z);
    for (new id; id<MAX_PLAYERS; id++)
        {
        if (!IsPlayerConnected(id)) continue;
            if(IsPlayerInRangeOfPoint(id, 20.0, x, y, z))  SendClientMessage(id, -1, message);
    }
   
    return 0;
}
Your code also didn't work because you don't get the players position .
Reply
#3

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
Your code also didn't work because you don't get the players position .
I suppose I left that out accidentally when I recoded it, and I don't think it being 3:03 AM after a sugarhigh made it better, but nevertheless, I thank you for pointing out the mistake.

I did try it with the MAX_PLAYERS variable [and the GetMaxPlayers() method], but it failed, as well, also with no errors / warnings (I tried it before I recoded).


EDIT: I believe I see why it didn't work, now... Thank you for your help.
Reply
#4

I've decided to use the "foreach" loop. I thank you for your constructive criticism, attitude, and help.

The script below is what the finished product (of this thread) is.


pawn Код:
#include <a_samp>
#include <foreach>

public OnPlayerText(playerid, text[]) {
    new name[256], message[256];
    new Float:x, Float:y, Float:z;

    GetPlayerName(playerid, name, sizeof(name));
    format(message, sizeof(message), "%s says, \"%s\"",  name, text);
   
    GetPlayerPos(playerid, x, y, z);
   
    foreach (new player : Player) {
        if (IsPlayerInRangeOfPoint(player, 20.0, x, y, z)) {
            SendClientMessage(player, -1, message);
        }
    }
   
    return 0;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)