[Tutorial] How to Make a Dice Command
#1

Hello And Welcome to my First Tutorial.
I'd like to show you how ot make a dice command.


Heres our Command
NOTE: add this on the top on your script "#include <strlib>"
NOTE: Download strlib here: rum.sa-mp.com/showthread.php?t=85697

pawn Code:
public onPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp("/dice", cmdtext))
   {
          // Do something Here
               return 1;
         }
         return 0;
}
What we'll do is to initialize a new variable for this.
and the variable is going to be "new dicenumber = random(6)"
the dice number is going to be equal to a new function which is called random
numbers and this is actually going to start at 0, so we want to know if it's
going to go from 1 to 6, that we're then going to is to add strings for this
once, so this is going to be STR dice number and it's going to have a maximum
amount of two character and then we're going to use
a function called valstr, which is going to be "valstr(dicenumber, dicenumber);
So now it looks like this.

pawn Code:
public onPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp("/dice", cmdtext))
   {
          new dicenumber = random(6) + 1; // this is the variable
          new strdicenumber[2] // the String
          valstr(dicenumber, dicenumber); // this is the valstr function
               return 1;
         }
         return 0;
}

The next we have to do is to send a client message we're going to send to the playerid.
and we're going ot use the green color "COLOR_GREEN", if you want to use that one,
then you have to go to the top of your script and Define the color "(Place it on the top of your script)", like this - "#Define COLOR_GREEN 0x33D02FFF", and then what we're going to use is one of the str live functions, and its the str_replace which im using, and the first parameter actually is "DICENUMBER" and then we want to replace it with something and what we will replace it with is the "strdicenumber" which we used before. and thats
why we're going to use valstr because valstr is going to put the dicenumber in this string called "new strdicenumber[2];" and then going to search for the "DICENUMBER" in a string, now we're going to replace the dicenumber string with the actual number and then we're going to finish off this with a
sentence, we'll then do this - "strdicenumber, "You rolled #DICENUMBER);" and now its going to take this "DICENUMBER" since we edited this "str_replace("DICENUMBER"", and its going to replace
it with the actual "strdicenumber" which first was an integer that then was stored using valstr as a
string.


pawn Code:
public onPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp("/dice", cmdtext))
   {
    new dicenumber = random(6) + 1; // this is the variable
    new strdicenumber[2] // the String
    valstr(strdicenumber, dicenumber); // this is the valstr function
  SendClientMessage(playerid, COLOR_GREEN, str_replace("DICENUMBER", strdicenumber, "You rolled #DICENUMBER);
               return 1;
         }
         return 0;
}
So this is going to, whenever the player /dice would say, "You Rolled the dicenumber." which
we are going to make now. and lastly we're going to learn a new function and this is
a loop and the loop we're going to use is a For() loop. so what we have to do is to
initalize a new variable and im going to call this one, "player" and we're going to loop through all the player ids
and it always starts at 0 so im going to initalize it to 0, and then im going to have
a condition because the for() loop always have 1 variable. and the condition is going to be
player is less the MAX_PLAYERS; but we're not using ; at the end, the current code should be
for(new player = 0; player < MAX_PLAYERS; player++){

pawn Code:
public onPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp("/dice", cmdtext))
   {
    new dicenumber = random(6) + 1; // this is the variable
    new strdicenumber[2] // the String
    valstr(strdicenumber, dicenumber); // this is the valstr function
  SendClientMessage(playerid, COLOR_GREEN, str_replace("DICENUMBER", strdicenumber, "You rolled #DICENUMBER);
   for(new player = 0; player < MAX_PLAYERS; player++){

 
              }
               return 1; // The return statement is going to signal to the command was executed properly
         }
         return 0; // the return 0; is not going to be executed.
}
So what we're going to do now instead of the playerid is to Send a client message,
like this - "SendClientMessage" instead of using "playerid" we're going to use this "player",
so it should look like this, "SendClientMessage(player, COLOR_RED, str_replace("DICENUMBER", strdicenumber, "Someone rolled #DICENUMBER);" so we used the same thing as above. to get the red color define this
as well, as i told you above. #define COLOR_RED 0xC60000FF ,

pawn Code:
public onPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp("/dice", cmdtext))
   {
    new dicenumber = random(6) + 1; // this is the variable
    new strdicenumber[2] // the String
    valstr(strdicenumber, dicenumber); // this is the valstr function
  SendClientMessage(playerid, COLOR_GREEN, str_replace("DICENUMBER", strdicenumber, "You rolled #DICENUMBER);
   for(new player = 0; player < MAX_PLAYERS; player++){
   SendClientMessage(player, COLOR_RED, str_replace("
DICENUMBER", strdicenumber, "Someone rolled #DICENUMBER);

 
              }
               return 1; // The return statement is going to signal to the command was executed properly
         }
         return 0; // the return 0; is not going to be executed.
}
Thanks, this was my first tutorial, +rep and please comment
Reply
#2

valstr(dicenumber, dicenumber)

should be ?
valstr(strdicenumber, dicenumber)

and why dont u just use SendClientMessageToAll instead of looping each player? lol

dice is pretty simple to make its good i guess


also u dont need include lib to do this its not neccesary just use format
Reply
#3

Quote:
Originally Posted by Ihateyou
View Post
valstr(dicenumber, dicenumber)

should be ?
valstr(strdicenumber, dicenumber)

and why dont u just use SendClientMessageToAll instead of looping each player? lol

dice is pretty simple to make its good i guess


also u dont need include lib to do this its not neccesary just use format
Thanks, and thanks for your suggestions.
Reply
#4

Thanks for this useful tutorial ! +rep
Reply
#5

Quote:
Originally Posted by streetpeace
View Post
Thanks for this useful tutorial ! +rep
Thanks, Appreciated.
Reply
#6

bump
Reply
#7

bump
Reply
#8

bump
Reply
#9

I need some thoughts about my tutorial :/, Bump.
Reply
#10

bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)