It would be high appreciated
#1

Just scripted a cmd /setcolor from a player, but Since I am a beginner, and don't know how to do it exactly as I want, please help me to improve it.

Quote:

CMDetcolor(playerid, params[])
{
if (!IsPlayerAdmin(playerid))
{
SendClientMessage(playerid, -1, "SERVER: Unknown command.");
}
else if (IsPlayerAdmin(playerid))
{
SetPlayerColor(playerid, COLOR_YELLOW);
SendClientMessage(playerid, COLOR_GREEN, "You succesfully changed the player his color.");
}
return 1;
}

The things I want to be added is the following: That the admin can do /setcolor [ID] [COLOR], and it worked for me when I just did /setcolor, but when you just type as a admin/setcolor it should say "USAGE: /Setcolor [ID] [COLOR]
Reply
#2

I invite you to have a look at the tutorial section, look for commands + sscanf tutorials.

I'll give you an example:
pawn Code:
CMD:setcolor(playerid, params[])
{
    new id, colour;
   
    if (IsPlayerAdmin(playerid))
        SendClientMessage(playerid, -1, "SERVER: Unknown command.");
       
    else if (sscanf(params, "ud", id, colour))
        SendClientMessage(playerid, -1, "USAGE: /setcolor [ player id ] [ colour ]");
       
    else if (id == INVALID_PLAYER_ID)
        SendClientMessage(playerid, -1, "Requested player was not found!");
       
    else
    {
        SetPlayerColor(id, colour);
        SendClientMessage(playerid, COLOR_GREEN, "You succesfully changed the player's colour.");
    }
    return 1;
}
Reply
#3

Using sscanf is a very common thing to use along with ZCMD, and I would recommend it. If you don't already have it installed, you can download it here: https://sampforum.blast.hk/showthread.php?tid=120356.

I tried explaining why I had done what, if you need more explanation on it I can do so.

Code:
CMD:setcolor(playerid, params[])
{
	if (!IsPlayerAdmin(playerid)) SendClientMessage(playerid, -1, "SERVER: Unknown command.");
	else if (IsPlayerAdmin(playerid))
	{
		new iTargetID, iColor; //define the variables we want the data from the command going to (written after /setcolor in-game)
		if(sscanf(params, "ud", iTargetID, iColor)) 
		{
			SendClientMessage(playerid, -1, "USAGE: /setcolor [playerid] [color]"); //In sscanf, 'u' is a player id and 'd' is an integer
			SendClientMessage(playerid, -1, "Colors: 1 - Yellow, 2 - White, 3 - Red");
		}
		/* a switch statement is like multiple 'if' and 'else if' statements however compressed to look like this rather than  
		if(iColor == 1) iColor = 0xFFFF0000;
		if(iColor == 2) iColor = 0xFFFFFF00;
		if(iColor == 3) iColor = 0xFF0606FF;
		else iColor = 0xFFFFFF00;
		
		Just like if statements, brackets are optional if it is only 1 line.
		*/
		if(!IsPlayerConnected(iTargetID)) return SendClientMessage(playerid, -1, "The specified player is not connected.");
		switch(iColor)
		{
			case 1: iColor = 0xFFFF0000;
			case 2: iColor = 0xFFFFFF00;
			case 3: iColor = 0xFF0606FF;
			default: iColor = 0xFFFFFF00; //default will set the color as white if the number isnt 1, 2 or 3.
		}
		SetPlayerColor(iTargetID, iColor);
		SendClientMessage(playerid, COLOR_GREEN, "You succesfully changed the player his color.");
	}
	return 1;
}
Reply
#4

I used Miguel his code, and It works, but when I do /setcolor [ID] [Color] ig, the color doesn't work.

lets say I have a orange color, and I do /setcolor 0 purple, that doesn't work.
everything works except the color
Reply
#5

No man,
Miguel script is by Color id
you shouldn't use the name of color you have to use color id
Reply
#6

As you can see, my code receives the colour as an integer number. If you want to receive the colour as a string (text), you'll have to work the sscanf line a bit. Read some sscanf tutorials and learn how to use the strcmp function, which compares two character strings.

You're command should work like this:

- Checks if the player entered a valid player and a string.
- Compares if that string is the same as a known colour (you should have a list of colours).
- If it is the same as a known colour, then SetPlayerColour to the desired colour.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)