SA-MP Forums Archive
How to detect when someone is clicking a button? / Command Zones? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to detect when someone is clicking a button? / Command Zones? (/showthread.php?tid=463083)



How to detect when someone is clicking a button? / Command Zones? - Jason_Roul - 10.09.2013

I've seen people using that kind of scripts where you can detect when someone clicks a button on the keyboard it reacts in a certain how would you do that?

Also, if anyone doesn't mind explain how can you create a certain zone that a command can only happen in?


Re: How to detect when someone is clicking a button? / Command Zones? - Ceez - 10.09.2013

How to detect a button being pressed:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
Here's a link to all of the KEY_'s
https://sampwiki.blast.hk/wiki/GetPlayerKeys#Key_List

To detect if a command made in an area / zone:
pawn Код:
if(IsPlayerInRangeOfPoint(playerid, RANGE, PosX, PosY, PosZ))
{
      .... // code
}
Replace RANGE with the max distance from player to points ( PosX, Y and Z).

Good luck! REP if I helped


Re: How to detect when someone is clicking a button? / Command Zones? - Jason_Roul - 10.09.2013

Is it really needed to have an old key? What if he clicks it at any point while he hasn't had clicked any button, by that I mean what if he is afk and he comes back and clicks a button or he connects and its the first button he presses.


Re: How to detect when someone is clicking a button? / Command Zones? - Ceez - 10.09.2013

No don't have to use an oldkey
you can use something like this
pawn Код:
if(!IsPlayerInAnyVehicle(playerid))
    {
        if(newkeys == KEY_YES)
        {
              SendClientMessage(playerid, -1, " You are not inside a vehicle. ");
        }
      }
This function says: if a player isn't inside a vehicle and he taps KEY_YES (known as Y) it sends him message he isn't inside a vehicle.


Re: How to detect when someone is clicking a button? / Command Zones? - Dragonsaurus - 10.09.2013

No, oldkey is optional. Detects the player's old pressed key. Just check for the new as Ceez said and you'll be fine


Re: How to detect when someone is clicking a button? / Command Zones? - Jason_Roul - 10.09.2013

One last question! If I make it only new_key do I need to use old_key after or does it keep detecting automatically the old one? :P


Re: How to detect when someone is clicking a button? / Command Zones? - Dragonsaurus - 10.09.2013

Not at all, btw the correct usage is:
pawn Код:
if(newkeys & KEY_FIRE)
// or
if(oldkeys & KEY_FIRE)
Use the first if you want to use only the newkeys.
SA-MP system makes the hard job.


Re: How to detect when someone is clicking a button? / Command Zones? - Ceez - 10.09.2013

let me explain you something
newkeys mean if the player has changed his keys to something else it will use them (if a player's KEY_YES button was Y and he changed to Z, he'll need to click Z for the KEY_YES.)

oldkeys mean if the player has changed/didnt change, key stays the same (if key is KEY_YES, it will stay Y no matter if player changed it or no)