SA-MP Forums Archive
[Tutorial] Doing basic commands on another player(s). - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Doing basic commands on another player(s). (/showthread.php?tid=208304)



Doing basic commands on another player(s). - Haydz - 08.01.2011

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.


Re: Doing basic commands on another player(s). - Tessar - 08.01.2011

Nice Tutorial, Really Basic and easy to understand.


Re: Doing basic commands on another player(s). - jameskmonger - 08.01.2011

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)


Re: Doing basic commands on another player(s). - Lorenc_ - 09.01.2011

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


Re: Doing basic commands on another player(s). - ToPhrESH - 09.01.2011

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.


Re: Doing basic commands on another player(s). - rubygta - 10.01.2011

Good Job


Re: Doing basic commands on another player(s). - Hiddos - 10.01.2011

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


Re: Doing basic commands on another player(s). - HyperZ - 11.01.2011

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

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


Re: Doing basic commands on another player(s). - Haydz - 11.01.2011

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!


Re: Doing basic commands on another player(s). - XRVX - 11.01.2011

nice tutorial man, good job


Re: Doing basic commands on another player(s). - Balcan Fox - 11.01.2011

Great tutorial, good job man.Thanks !


Re: Doing basic commands on another player(s). - Tee - 11.01.2011

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:




Re: Doing basic commands on another player(s). - Marty_Alex - 21.01.2011

I like it easy to understand thx for the help mate


Re: Doing basic commands on another player(s). - Kyro - 20.02.2011

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


Re : Doing basic commands on another player(s). - [WTF]Godfather - 20.02.2011

Good work , nice tutorial


Re: Doing basic commands on another player(s). - Haydz - 21.02.2011

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



Re: Doing basic commands on another player(s). - tanush - 24.02.2011

why my server gets restarted when i type the cmd?


Re: Doing basic commands on another player(s). - rinori - 27.02.2011

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.


Re: Doing basic commands on another player(s). - Mean - 27.02.2011

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.