[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
#2

Nice Tutorial, Really Basic and easy to understand.
Reply
#3

I knew how to do most of this, but it's really well written and teaches newer users how to do it. Nice job.

(wooooo 500 posts)
Reply
#4

Great. Now i can give this to my older brother to read, cbf explaining it to him lols, many thanks.
Reply
#5

This would be really good for "OnPlayerClickPlayer". Wow, thank alot Hayden. THis is a really helpful and explaining much!!!

5/5 man this is good.
Reply
#6

Good Job
Reply
#7

No offense, but you should really explain more about ZCMD and sscanf.
Reply
#8

pawn Код:
if(sscanf(params, "d", otherplayerid))

Hmm, How about this?
pawn Код:
if(sscanf(params, "u", otherplayerid))
Nice TUT btw.
Reply
#9

Quote:
Originally Posted by Clive
Посмотреть сообщение
pawn Код:
if(sscanf(params, "d", otherplayerid))

Hmm, How about this?
pawn Код:
if(sscanf(params, "u", otherplayerid))
Nice TUT btw.
Yeh that is correct lol, i will change that now. Not very good with sccanf .

And thanks everyone eles for the comments!
Reply
#10

nice tutorial man, good job
Reply
#11

Great tutorial, good job man.Thanks !
Reply
#12

Quote:
Originally Posted by Hayden_Bruin
Посмотреть сообщение
Yeh that is correct lol, i will change that now. Not very good with sccanf .

And thanks everyone eles for the comments!
This might not be the best help, but i learned alot from this lil table below:

Reply
#13

I like it easy to understand thx for the help mate
Reply
#14

Quote:
Originally Posted by Hayden_Bruin
View Post
if(sscanf(params, "d", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /hi if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /whatareournames
thank u soo much just what i needed

Can u plz explain why we put "d" in the first command

and why "u" in the second one please
ty
Reply
#15

Good work , nice tutorial
Reply
#16

Quote:
Originally Posted by Kyro
View Post
thank u soo much just what i needed

Can u plz explain why we put "d" in the first command

and why "u" in the second one please
ty
my bad, it should be a u in both of them, fixed that up on the main page also.
pawn Code:
if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /hi id");
if(sscanf(params, "u", otherplayerid)) return SendClientMessage(playerid, colorhere, "Use /whatareournames
Reply
#17

why my server gets restarted when i type the cmd?
Reply
#18

I get this error when trying to open the server
Code:
[04:38:25]   Loading filter script 'gl_realtime.amx'...
[04:38:25]   Loading filter script 'gl_mapicon.amx'...
[04:38:25]   Loading filter script 'ls_elevator.amx'...
[04:38:25]   Loaded 6 filter scripts.

[04:38:25] Reading File: blank
[04:38:25] Reading File: properties/houses.txt
[04:38:25] Reading File: properties/businesses.txt
[04:38:25] Reading File: properties/banks.txt
[04:38:25] Reading File: properties/police.txt
[04:38:25] Script[gamemodes/test1.amx]: Run time error 19: "File or function is not found"
[04:38:25] Number of vehicle models: 0
I don't know but once I script the commands above, I can't run the server.
Reply
#19

This is called "Multi-Parameter command". In your title I mean somethink like executing commands on other players, like fakecmd, for example, I am ID 0, executed a /kill command on ID 1. But good tutorial.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)