IsPlayerConnected problem
#1

So I have a weird problem with (zcmd?) the IsPlayerConnected function. Here is some code:

PHP код:
CMD:givemoney(playeridparams[])
{
    new
        
targetID,
        
parameter
    
;
    if(
sscanf(params"ui"targetIDparameter)) return UsageTag(playerid"/givemoney [ID] [AMOUNT]");
    if(!
IsPlayerConnected(targetID)) return ErrorTag(playerid"The specified player is offline!");
    
GivePlayerCash(targetIDparameter);
    return 
1;

So my problem is when I type a non-connected ID with the command it just returns the unknown command thing. The really strange thing about is that this same exact command is worked before, but since I re-wrote it its just broken. Any suggestions?
Reply
#2

Could you show us the function ErrorTag?
Reply
#3

Look:
Код:
new 
        targetID, 
        parameter, 
    ;
Delete comma (,)after parameter.
Reply
#4

Sure, here:

PHP код:
stock ErrorTag(playeridmessage[])
{
    new 
string[128];
    
format(stringsizeof(string), ""TEXT_RED"[ERROR] "TEXT_WHITE"%s"message);
    
SendClientMessage(playeridREDstring);
    return 
1;

Quote:
Originally Posted by CherryMond
Посмотреть сообщение
Look:
Код:
new 
        targetID, 
        parameter, 
    ;
Delete comma (,)after parameter.
Nope, thats not it, I just deleted couple of lines before I posted this code. It would be an error if I complie it but I edited the post.

EDIT:

Here is the full code, I just figured it out its something else in this code even if the IsPlayerConnected line is correct:
PHP код:
CMD:givemoney(playeridparams[])
{
    new
        
targetID,
        
parameter,
        
string[128],
        
query[128]
    ;
    if(
pData[playerid][pAdmin] < 2) return ErrorTag(playerid"You have no permission to use this command!");
    if(
sscanf(params"ui"targetIDparameter)) return UsageTag(playerid"/givemoney [ID] [AMOUNT]");
    if(
parameter 0) return ErrorTag(playerid"You cant use minus value.");
    if(
pData[targetID][pIsLoggedIn] == false) return ErrorTag(playerid"The selected player is not logged in!");
    if(!
IsPlayerConnected(targetID)) return SendClientMessage(playerid0xFFFFFF"Test");
    
mysql_format(g_sqlquerysizeof(query), "UPDATE characters SET p_money = '%d' WHERE p_name = '%e'"pData[playerid][pMoney], ReturnName(targetID));
    
mysql_tquery(g_sqlquery);
    
format(stringsizeof(string), "%s gave you $%d"ReturnName(playerid), parameter);
    
ServerTag(targetIDstring);
    
format(stringsizeof(string), "You gave %s $%d"ReturnName(targetID), parameter);
    
ServerTag(playeridstring);
    
GivePlayerCash(targetIDparameter);
    return 
1;

EDIT 2:

Its fixed, I just needed to move the IsPlayerConnected line directly below the sscanf one.
Reply
#5

mysql_format(g_sql, query, sizeof(query), "UPDATE characters SET p_money = '%d' WHERE p_name = '%e'", pData[playerid][pMoney], ReturnName(targetID));

'pData[playerid][pMoney]' should be 'pData[targetID][pMoney]'

By the way, it should be like this:

PHP код:
pData[targetID][pMoney] += parameter;
ResetPlayerMoney(targetID);
GivePlayerMoney(targetIDpData[targetID][pMoney]);
mysql_format(g_sqlquerysizeof(query), "UPDATE characters SET p_money = '%d' WHERE p_name = '%e'"pData[targetID][pMoney], ReturnName(targetID)); 
    
mysql_tquery(g_sqlquery); 
Reply
#6

Quote:
Originally Posted by Undef1ned
Посмотреть сообщение
mysql_format(g_sql, query, sizeof(query), "UPDATE characters SET p_money = '%d' WHERE p_name = '%e'", pData[playerid][pMoney], ReturnName(targetID));

'pData[playerid][pMoney]' should be 'pData[targetID][pMoney]'
Oh yeah, thats another problem. Much appreciated haha. Oh and yeah, I just forgot to update the enum, but that one is done already.
Reply
#7

Also, the IsPlayerConnected check should be above the pData[targetID][pIsLoggedIn] check, shouldn't it?
Because if you receive an Invalid Player ID with sscanf, then you will have an error when trying to access the array at that invalid index, I believe.

And you should use "r" instead of "u" for sscanf, because "u" also detects NPCs, which you don't want, right?
https://sampforum.blast.hk/showthread.php?tid=570927
Reply
#8

Quote:
Originally Posted by AdamsLT
Посмотреть сообщение
Also, the IsPlayerConnected check should be above the pData[targetID][pIsLoggedIn] check, shouldn't it?
Because if you receive an Invalid Player ID with sscanf, then you will have an error when trying to access the array at that invalid index, I believe.

And you should use "r" instead of "u" for sscanf, because "u" also detects NPCs, which you don't want, right?
https://sampforum.blast.hk/showthread.php?tid=570927
Yeah, IsPlayerConnected is already directly below the sscanf line, I just figured it out that was the problem. And Im probably not going to use any NPCs in the future but thank you for the useful tip!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)