Right mouse Button + F = Tazer!
#1

Hi guys, I need your help.
How can I convert that ONLY if a player click the right mouse button + F, that he can /tazer the other players?
I already have:

if ((newkeys & 16) & & (oldkeys & 12 & & (oldkeys & 16))

This does not work, can help me one?
Sincerely, Imagician.
Reply
#2

Follow the // commented lines:

pawn Код:
// Defining KEY_AIM (right mouse button), as it is not defined in the SA-MP includes)

// place at the top of your script:

#define KEY_AIM     (128)

// Under OnPlayerKeyStateChange

if(newkeys == KEY_AIM && newkeys == KEY_SECONDARY_ATTACK) // i think KEY_AIM is RMB and SECONDARY_ATTACK is the F key
{
    // tazer code here
    return 1;
}
Try it.
Reply
#3

Quote:
Originally Posted by grand.Theft.Otto
Посмотреть сообщение
Follow the // commented lines:

pawn Код:
// Defining KEY_AIM (right mouse button), as it is not defined in the SA-MP includes)

// place at the top of your script:

#define KEY_AIM     (128)

// Under OnPlayerKeyStateChange

if(newkeys == KEY_AIM && newkeys == KEY_SECONDARY_ATTACK) // i think KEY_AIM is RMB and SECONDARY_ATTACK is the F key
{
    // tazer code here
    return 1;
}
Try it.
https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange

Quote:
How NOT to check for a key

Let's presume that you want to detect when a player presses their FIRE button, the obvious code would be:
pawn Код:
if (newkeys == KEY_FIRE)
This code may even work in your testing, but it is wrong and your testing is insufficient. Try crouching and pressing fire - your code will instantly stop working. Why? Because "newkeys" is no longer the same as "KEY_FIRE", it is the same as "KEY_FIRE" COMBINED WITH "KEY_CROUCH".

How to check for a key

So, if the variable can contain multiple keys at once, how do you check for just a single one? The answer is bit masking. Each key has its own bit in the variable (some keys have the same bit, but they are onfoot/incar keys, so can never be pressed at the same time anyway) and you need to check for just that single bit:
pawn Код:
if (newkeys & KEY_FIRE)
Note that the single "&" is correct - this is a bitwise AND, not a logical AND, which is what the two ampersands are called.

Now if you test this code it will work whether you are crouching or standing when you press the fire key. However there is still one slight problem - it will fire as long as you are holding the key. OnPlayerKeyStateChange is called every time a key changes and that code is true whenever the the fire key is held down. If you press fire the code will fire, if that key is held and you press crouch - that code will fire again because a key (crouch) has changed and fire is still held down How do you detect when a key is first pressed, but not trigger again when it's still held and another key changes?
Reply
#4

Hmm, thanks. He can learn from there.

I'm not too familiar scripting/using keys to select things in my server.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)