How to use the random function properly
#1

hi, i wanna make a random trucking/taxi mission when a player enters a CP

to do that I made this code:
pawn Код:
if(IsPlayerInDynamicCP(playerid, STW))
    {
        if(IsWorking[playerid] != 0)
        {
            SendClientMessage(playerid, COLOR_RED, "You already have a job! finish it or type /kill");
            return 1;
        }
       
        if(vehicle != 516)
        {
            SendClientMessage(playerid, COLOR_RED, "You are not in the right vehicle for this mission!");
            return 1;
        }
       
        if(gTeam[playerid] == TEAM_DRIVERS)
        {
            IsWorking[playerid] = 1;

            TogglePlayerDynamicCP(playerid, STW, false); // This makes sure that there will be a new checkpoint

            //hier zullen dan de random moeten komen
            new mrand = random(2);
            if(mrand == 1)
            {

                TogglePlayerDynamicCP(playerid, TRAVJ1, true); // Creates a new checkpoint at a different position

                SetPlayerMapIcon( playerid, 2, 334.4390,-1519.8026,35.7015, 41, 0, MAPICON_GLOBAL );
                GameTextForPlayer(playerid,"~w~ Job Lvl 1~g~ :Travis Pastrana is going to participate the X-games",4000,3);
                SendClientMessage(playerid, COLOR_YELLOW, "|- Check the map to see the point where to go. -|");

                //De Textdraw
                TextDrawShowForPlayer(playerid,Opdracht);
            }
           
            else if(mrand == 2)
            {

                SendClientMessage(playerid, COLOR_YELLOW, "|- This is just a test -|");

            }
           
            return 1;
        }
    }
ok so as you can see I used if(mrand == 2 or 1)

but when I pickup the pickup nothing happens, I don't get the messages from the first random option, and nor do I get any message from the second random thing :/

please help me

thanks in advance
Reply
#2

Quote:
Originally Posted by knackworst
Посмотреть сообщение
hi, i wanna make a random trucking/taxi mission when a player enters a CP

to do that I made this code:
pawn Код:
if(IsPlayerInDynamicCP(playerid, STW))
    {
        if(IsWorking[playerid] != 0)
        {
            SendClientMessage(playerid, COLOR_RED, "You already have a job! finish it or type /kill");
            return 1;
        }
       
        if(vehicle != 516)
        {
            SendClientMessage(playerid, COLOR_RED, "You are not in the right vehicle for this mission!");
            return 1;
        }
       
        if(gTeam[playerid] == TEAM_DRIVERS)
        {
            IsWorking[playerid] = 1;

            TogglePlayerDynamicCP(playerid, STW, false); // This makes sure that there will be a new checkpoint

            //hier zullen dan de random moeten komen
            new mrand = random(2);
            if(mrand == 1)
            {

                TogglePlayerDynamicCP(playerid, TRAVJ1, true); // Creates a new checkpoint at a different position

                SetPlayerMapIcon( playerid, 2, 334.4390,-1519.8026,35.7015, 41, 0, MAPICON_GLOBAL );
                GameTextForPlayer(playerid,"~w~ Job Lvl 1~g~ :Travis Pastrana is going to participate the X-games",4000,3);
                SendClientMessage(playerid, COLOR_YELLOW, "|- Check the map to see the point where to go. -|");

                //De Textdraw
                TextDrawShowForPlayer(playerid,Opdracht);
            }
           
            else if(mrand == 2)
            {

                SendClientMessage(playerid, COLOR_YELLOW, "|- This is just a test -|");

            }
           
            return 1;
        }
    }
ok so as you can see I used if(mrand == 2 or 1)

but when I pickup the pickup nothing happens, I don't get the messages from the first random option, and nor do I get any message from the second random thing :/

please help me

thanks in advance
Try reading this better:
https://sampwiki.blast.hk/wiki/Random

Quote:

A random number ranging from 0 to max-1.

So your code:
pawn Код:
if(IsPlayerInDynamicCP(playerid, STW))
    {
        if(IsWorking[playerid] != 0)
        {
            SendClientMessage(playerid, COLOR_RED, "You already have a job! finish it or type /kill");
            return 1;
        }
       
        if(vehicle != 516)
        {
            SendClientMessage(playerid, COLOR_RED, "You are not in the right vehicle for this mission!");
            return 1;
        }
       
        if(gTeam[playerid] == TEAM_DRIVERS)
        {
            IsWorking[playerid] = 1;

            TogglePlayerDynamicCP(playerid, STW, false); // This makes sure that there will be a new checkpoint

            //hier zullen dan de random moeten komen
            new mrand = random(3);
            if(mrand == 1)
            {

                TogglePlayerDynamicCP(playerid, TRAVJ1, true); // Creates a new checkpoint at a different position

                SetPlayerMapIcon( playerid, 2, 334.4390,-1519.8026,35.7015, 41, 0, MAPICON_GLOBAL );
                GameTextForPlayer(playerid,"~w~ Job Lvl 1~g~ :Travis Pastrana is going to participate the X-games",4000,3);
                SendClientMessage(playerid, COLOR_YELLOW, "|- Check the map to see the point where to go. -|");

                //De Textdraw
                TextDrawShowForPlayer(playerid,Opdracht);
            }
           
            else if(mrand == 2)
            {

                SendClientMessage(playerid, COLOR_YELLOW, "|- This is just a test -|");

            }
           
            return 1;
        }
    }
if you want to random like 5 numbers it should be random(6)
its always +1 of the number you want to random
Reply
#3

Better use switch:
pawn Код:
switch(random(2))
{
    case 0:
    {
        TogglePlayerDynamicCP(playerid, TRAVJ1, true); // Creates a new checkpoint at a different position
        SetPlayerMapIcon( playerid, 2, 334.4390,-1519.8026,35.7015, 41, 0, MAPICON_GLOBAL );
        GameTextForPlayer(playerid,"~w~ Job Lvl 1~g~ :Travis Pastrana is going to participate the X-games",4000,3);
        SendClientMessage(playerid, COLOR_YELLOW, "|- Check the map to see the point where to go. -|");
        //De Textdraw
        TextDrawShowForPlayer(playerid,Opdracht);
    }
    case 1: SendClientMessage(playerid, COLOR_YELLOW, "|- This is just a test -|");
}
Reply
#4

It will be less buggy and faster using switch? Or just rasier to use?
Reply
#5

Quote:
Originally Posted by knackworst
Посмотреть сообщение
It will be less buggy and faster using switch? Or just rasier to use?
faster.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)