[Tutorial] Doing basic commands on another player(s).
#1

Table of contents.


This tutorial was made because i saw somebody ask how to get a another players name and i also had trouble doing commands on other players when i first started.

1 - Doing a basic command on another player.
2 - Doing a basic command on another player displaying there name.

Note: before starting this tut you will need the ZCMD include and the sscanf include.

1 - Doing a basic command on another player
This is a basic command when will send a message to the players id/name you type and kill them at the same time.
pawn Код:
COMMAND:hi(playerid, params[]) //using ZCMD this is how your command will start off looking like.
{
    new otherplayerid;
    if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /hi [playerid/name]");
    else if(!IsPlayerConnected(otherplayerid)) return SendClientMessage(playerid, colorhere, "This player is not connected");
    else
    {
        SendClientMessage(otherplayerid,colorhere,"Hey man, I'm going to kill you lol!");
        SetPlayerHealth(otherplayerid,0);
    }
    return 1;
}
Lets break this up.
pawn Код:
new otherplayerid;
"otherplayerid" means the other players id which we typed in, e.g /hi 5 will send a message to id 5 and then kill them straight after, /hi 2 will send a message to id 2 and kill them straight after, etc etc.

pawn Код:
if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /hi [playerid/name]");
Spilts the strings.

pawn Код:
else if(!IsPlayerConnected(otherplayerid)) return SendClientMessage(playerid, colorhere, "This player is not connected");
When using a command on another players id you will always need to check if there connected. if the player is not connected it will excute the command, if they are not connected it will stop the command and return "This player is not connected"

pawn Код:
else
If the command the player trys is sucessfull (meaning that the player is connected) it will excute this command below.

pawn Код:
SendClientMessage(otherplayerid,colorhere,"Hey man, i'm going to kill you lol!"); // sends a message to the other player, "id"
SetPlayerHealth(otherplayerid,0); // will set the other players health to 0, and kill them.
You can place anything here, but for now we want to message saying we are going to kill them and kill them.

2 - doing basic commands on other players displaying there name.

we will be using this command to find out the other players name and display it in text with a clientmessage.
pawn Код:
COMMAND:whatareournames(playerid, params[])
{
    if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /whatareournames [playerid/name]");
    else if(!IsPlayerConnected(otherplayerid)) return SendClientMessage(playerid, colorhere, "This player is not connected");
    else
    {
        new otherplayerid,gName[25],pName[25],string[100];
        GetPlayerName(playerid, pName, 24);
        GetPlayerName(otherplayerid, gName, 24);
        format(string, sizeof(string),"His name is %s and yours is %s,",gName,pName);
        SendClientMessage(playerid, yourcolor,string);
    }
    return 1;
}
Lets break this up.
pawn Код:
if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /whatareournames [playerid/name]");
Spilts the strings.

pawn Код:
else if(!IsPlayerConnected(otherplayerid)) return SendClientMessage(playerid, colorhere, "This player is not connected");
If the player is connected the command will go through, if not it will return "This player is not connected"

pawn Код:
else
if the command passes all the checks the final part of the command will be excuted.

pawn Код:
new otherplayerid,gName[25],pName[25],string[100];
otherplayerid - means the id of the player that we are using the command on.
gName - this is what we will be using to display the other players name.
pName - This is what we will be using to display our name.
string - You always need to create a string if you're going to use format to display text. note - You don't have to call it "string" it can be called almost anything.

pawn Код:
GetPlayerName(playerid, pName, 24);
playerid - playerid means the player who used the command.
pName - what we will be using to display our name(the player who used the command).
24 - the max characters in a username is 24 so thats the max possible string we may need.

pawn Код:
GetPlayerName(otherplayerid, gName, 24);
otherplayerid - the id of the player we are using the command on.
gName - the other players name, we can now use gName to display his username.
24 - as above, the max username limit is 24.

pawn Код:
format(string, sizeof(string),"His name is %s and yours is %s,",gName,pName);
SendClientMessage(playerid, yourcolor,string);
When displaying text you use %s in this case we want to display text so we will use %s to display the names.
You can find out more about 'format' on the wiki https://sampwiki.blast.hk/wiki/Format

End
This is my first tutorial so If you see anything i could improve on myself or a better way of doing a certain code which could improve the tutorial feel free to post. Cheers for reading.
Reply


Messages In This Thread
Doing basic commands on another player(s). - by Haydz - 08.01.2011, 08:27
Re: Doing basic commands on another player(s). - by Tessar - 08.01.2011, 08:52
Re: Doing basic commands on another player(s). - by jameskmonger - 08.01.2011, 08:54
Re: Doing basic commands on another player(s). - by Lorenc_ - 09.01.2011, 01:07
Re: Doing basic commands on another player(s). - by ToPhrESH - 09.01.2011, 23:21
Re: Doing basic commands on another player(s). - by rubygta - 10.01.2011, 18:13
Re: Doing basic commands on another player(s). - by Hiddos - 10.01.2011, 19:20
Re: Doing basic commands on another player(s). - by HyperZ - 11.01.2011, 06:40
Re: Doing basic commands on another player(s). - by Haydz - 11.01.2011, 08:16
Re: Doing basic commands on another player(s). - by XRVX - 11.01.2011, 09:59
Re: Doing basic commands on another player(s). - by Balcan Fox - 11.01.2011, 14:58
Re: Doing basic commands on another player(s). - by Tee - 11.01.2011, 23:48
Re: Doing basic commands on another player(s). - by Marty_Alex - 21.01.2011, 04:10
Re: Doing basic commands on another player(s). - by Kyro - 20.02.2011, 08:59
Re : Doing basic commands on another player(s). - by [WTF]Godfather - 20.02.2011, 20:25
Re: Doing basic commands on another player(s). - by Haydz - 21.02.2011, 06:35
Re: Doing basic commands on another player(s). - by tanush - 24.02.2011, 01:08
Re: Doing basic commands on another player(s). - by rinori - 27.02.2011, 02:37
Re: Doing basic commands on another player(s). - by Mean - 27.02.2011, 12:19

Forum Jump:


Users browsing this thread: 1 Guest(s)