Making a command work with a key. -
Johnson_Brooks - 16.06.2014
I would like to know how can i make the below command work with a key & get the closest player
The key i would like to use is number 2 .
pawn Код:
CMD:infect(playerid,params[])
{
if(gTeam[playerid] == TEAM_ZOMBIES)
{
new string[128];
new targetid;
new pName[MAX_PLAYER_NAME];
new tName[MAX_PLAYER_NAME];
if(!IsPlayerConnected(targetid)) return NOACCESS
if(sscanf(params,"u",targetid)) return xSCM"{FFA500}*{EEEEEE}Usage:/infect [playerid]");
GetPlayerName(playerid,pName,sizeof(pName));
GetPlayerName(targetid,tName,sizeof(tName));
format(string,sizeof(string),"{FFA500}*%s has infected %s and now he's a zombie",pName,tName);
SendClientMessageToAll(COLOR_WHITE,string);
gTeam[targetid] = TEAM_ZOMBIES;
SetPlayerColor(targetid,COLOR_ORANGE);
SetPlayerPos(targetid,-273.5326,1152.8778,20.7803);
SetPlayerHealth(targetid,100.0);
}
else xSCM"{FFA500}*{EEEEEE}You're not a zombie!");
return 1;
}
EDIT: I updated the command , and you should know that i haven't added a function that will check if the player is near another player .
Respuesta: Making a command work with a key. -
Zume - 16.06.2014
Try as well
pawn Код:
CMD:infect(playerid,params[])
{
if(gTeam[playerid] == TEAM_ZOMBIES){
new Float:MyPos[3], string[128], pName[MAX_PLAYER_NAME], tName[MAX_PLAYER_NAME];
if(sscanf(params,"u",params[0])) return xSCM"{FFA500}*{EEEEEE}Usage:/infect [playerid]");
if(!IsPlayerConnected(params[0])) return SendClientMessage(playerid, -1, "ID No coneected!");
GetPlayerPos(params[0], MyPos[0], MyPos[1], MyPos[2]);
if(IsPlayerInRangeOfPoint(playerid, 4.0, MyPos[0], MyPos[1], MyPos[2]) && GetPlayerVirtualWorld(playerid) == GetPlayerVirtualWorld(params[0]))
{
GetPlayerName(playerid,pName,sizeof(pName)), GetPlayerName(params[0],tName,sizeof(tName));
format(string,sizeof(string),"{FFA500}*%s has infected %s and now he's a zombie",pName,tName);
SendClientMessageToAll(COLOR_WHITE,string);
gTeam[params[0]] = TEAM_ZOMBIES;
SetPlayerColor(params[0],COLOR_ORANGE);
SetPlayerPos(params[0],-273.5326,1152.8778,20.7803);
SetPlayerHealth(params[0],100.0);
} else SendClientMessage(playerid, -1, "you not this near of the player");
} else SendClientMessage(playerid, -1, "{FFA500}*{EEEEEE}You're not a zombie!");
return 1;
}
Re: Making a command work with a key. -
iFiras - 17.06.2014
Now, the command performs to the closest player (No need to type the ID)
pawn Код:
CMD:infect(playerid,params[])
{
if(gTeam[playerid] == TEAM_ZOMBIES)
{
new string[128];
new targetid = GetClosestPlayer(playerid);
new pName[MAX_PLAYER_NAME];
new tName[MAX_PLAYER_NAME];
if(!IsPlayerConnected(targetid)) return NOACCESS
GetPlayerName(playerid,pName,sizeof(pName));
GetPlayerName(targetid,tName,sizeof(tName));
if(GetDistanceBetweenPlayers(playerid,targetid) < 4)
{
format(string,sizeof(string),"{FFA500}*%s has infected %s and now he's a zombie",pName,tName);
SendClientMessageToAll(COLOR_WHITE,string);
gTeam[targetid] = TEAM_ZOMBIES;
SetPlayerColor(targetid,COLOR_ORANGE);
SetPlayerPos(targetid,-273.5326,1152.8778,20.7803);
SetPlayerHealth(targetid,100.0);
}
}
else xSCM"{FFA500}*{EEEEEE}You're not a zombie!");
return 1;
}
You need this stock to get the closest player: (Put it at the bottom of your script)
pawn Код:
stock GetClosestPlayer(playerid)
{
new Float:cdist, targetid = -1;
for (new i = 0; i < MAX_PLAYERS; i++)
{
if (IsPlayerConnected(i) && GetPlayerState(i) != PLAYER_STATE_SPECTATING && playerid != i && (targetid < 0 || cdist > GetDistanceBetweenPlayers(playerid, i)))
{
targetid = i;
cdist = GetDistanceBetweenPlayers(playerid, i);
}
}
return targetid;
}
For the key thing, use
OnPlayerKeyStateChange.
Re: Making a command work with a key. -
Jefff - 17.06.2014
pawn Код:
CMD:infect(playerid,params[])
{
if(gTeam[playerid] == TEAM_ZOMBIES)
{
new targetid;
if(sscanf(params,"u",targetid)) xSCM"{FFA500}*{EEEEEE}Usage:/infect [playerid]");
else if(targetid == INVALID_PLAYER_ID) NOACCESS
else{
new string[128], pName[MAX_PLAYER_NAME], tName[MAX_PLAYER_NAME];
GetPlayerName(playerid,pName,sizeof(pName));
GetPlayerName(targetid,tName,sizeof(tName));
format(string,sizeof(string),"{FFA500}*%s has infected %s and now he's a zombie",pName,tName);
SendClientMessageToAll(COLOR_WHITE,string);
gTeam[targetid] = TEAM_ZOMBIES;
SetPlayerColor(targetid,COLOR_ORANGE);
SetPlayerPos(targetid,-273.5326,1152.8778,20.7803);
SetPlayerHealth(targetid,100.0);
}
}
else xSCM"{FFA500}*{EEEEEE}You're not a zombie!");
return 1;
}
GetClosestPlayer(playerid)
{
new Float:X,Float:Y,Float:Z;
GetPlayerPos(playerid,X,Y,Z);
for(new i=0; i != MAX_PLAYERS; i++)
if(IsPlayerConnected(i) && i != playerid)
if(IsPlayerStreamedIn(i, playerid) && IsPlayerInRangeOfPoint(i, 2.0, X, Y, Z))
return i;
return INVALID_PLAYER_ID;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(gTeam[playerid] == TEAM_ZOMBIES)
{
if(newkeys&KEY_SUBMISSION) // KEY_SUBMISSION is "2" ?
{
new ID = GetClosestPlayer(playerid);
if(ID != INVALID_PLAYER_ID && gTeam[ID] != TEAM_ZOMBIES)
{
new val[5];
valstr(val,ID);
return !cmd_infect(playerid,val);
}
}
}
return 1;
}
Re: Making a command work with a key. -
Threshold - 17.06.2014
Use:
pawn Код:
CMD:infect(playerid,params[])
{
if(gTeam[playerid] != TEAM_ZOMBIES) return xSCM"{FFA500}*{EEEEEE}You're not a zombie!");
new targetid;
if(sscanf(params, "u", targetid)) return xSCM"{FFA500}*{EEEEEE}Usage:/infect [playerid]");
if(targetid == INVALID_PLAYER_ID || !IsPlayerConnected(targetid) || targetid == playerid) return NOACCESS;
if(gTeam[targetid] == TEAM_ZOMBIES) return xSCM"{FFA500}*{EEEEEE}They are already a zombie!");
new string[100], pName[MAX_PLAYER_NAME], tName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof(pName));
GetPlayerName(targetid, tName, sizeof(tName));
format(string, sizeof(string), "{FFA500}*%s has infected %s and now they're a zombie", pName, tName);
SendClientMessageToAll(COLOR_WHITE, string);
gTeam[targetid] = TEAM_ZOMBIES;
SetPlayerColor(targetid, COLOR_ORANGE);
SetPlayerPos(targetid, -273.5326, 1152.8778, 20.7803);
SetPlayerHealth(targetid, 100.0);
return 1;
}
With the Jeff's GetClosestPlayer and OnPlayerKeyStateChange. Take note that Jeff's GetClosestPlayer is suited to get a player within 2.0 units of 'playerid'.
Re: Making a command work with a key. -
Johnson_Brooks - 17.06.2014
Thanks all , but here's a problem : When i press 2 , it doesn't work when i press N or shift or other keys it works.
What could be wrong ?
Re: Making a command work with a key. -
Jefff - 17.06.2014
You can't use '2' on foot is only for vehicles try for example if(newkeys&KEY_YES) its 'Y'
Re: Making a command work with a key. -
Johnson_Brooks - 17.06.2014
Whoa , really?
I didn't know . What key could i use onfoot ?
Re : Making a command work with a key. -
Chipardeur - 17.06.2014
Why not use lRMB ?