Detecting player for holding key.
#1

So i tryed this code under OnPlayerKeyStateChange:

pawn Код:
if(HOLDING(KEY_FIRE))
{
    SendClientMessage(playerid,COLOR_RED,"SERVER: Your holding LMB, good job.");
}
But, it only sends a message once (when i pressed) but this is suppose to detect 'HOLDING' so why doesn't it spam my chat with that message? Am i doing something wrong? Please help me, thank you.
Reply
#2

My guess is you forgot to place:

pawn Код:
return 1;
Reply
#3

Nope, just tryed it, it's the same. If i want the message to spam my chat... I have to be clickin like a maniac (and that's not holding) if i want it to do that.

Maybe i need a timer, and GetPlayerKeys, anybody got an idea? Because i tryed something and also didn't work.
Reply
#4

The timer would be a bad idea because it would just lag the hell out of the server I believe, lol.

I'm clueless though since I'm new to scripting, sorry man
Reply
#5

Using while...?
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    while(HOLDING(KEY_FIRE)) SendClientMessage(playerid, COLOR_RED, "SERVER: You're holding LMB, good job.");
    return 1;
}
I don't know...
Reply
#6

That's because OnPlayerKeyStateChange is only called when a player's key state changes. If you're holding the same key and doing nothing else, it won't spam your chatbox with that message.
Reply
#7

Quote:
Originally Posted by Tannz0rz
Посмотреть сообщение
That's because OnPlayerKeyStateChange is only called when a player's key state changes. If you're holding the same key and doing nothing else, it won't spam your chatbox with that message.
Than how to do it.
Reply
#8

Quote:
Originally Posted by Whizion
Посмотреть сообщение
Than how to do it.
Something along the lines of:
pawn Код:
new
    bool:HoldingKey[MAX_PLAYERS];
   
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY && !HoldingKey[playerid])
    {
        KeyCheck(playerid);
        HoldingKey[playerid] = true;
    }

    return 1;
}

forward KeyCheck(playerid);
public KeyCheck(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        new
            keys, ud, lr;
           
        GetPlayerKeys(playerid, keys, ud, lr);
       
        if(keys & KEY)
        {
            // Player is holding the key if it gets here
       
            SetTimerEx("KeyCheck", 100, 0, "i", playerid);
           
            return 0;
        }
    }
   
    HoldingKey[playerid] = false;
   
    return 0;
}
Reply
#9

I'll try it, thank you.
Reply
#10

Update: It works perfectly, thanks again.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)