Spectating a random player
#1

Lol i don't know it this works ... but i don't know much about random()

pawn Код:
for(new i; i<MAX_PLAYERS; i++)
            {
                new randomplayer = random(30);
                TogglePlayerSpectating(playerid, 1);
                PlayerSpectateVehicle(playerid,randomplayer);
            }
I don't think this is working ... i can't test it right now xd

plz help me

UPDATE:
pawn Код:
new randomplayer = Iter_Random(Player);
        TogglePlayerSpectating(playerid, 1);
        PlayerSpectateVehicle(playerid,GetPlayerVehicleID(randomplayer));
THIS SHOULD WORK ?!
Reply
#2

Have you got 30 players on your server when testing? The random function in your script will just get a random number 0 - 29, it doesn't check if the player is actually connected. Easiest way i can think of is using foreach.
pawn Код:
new randomplayer = Iter_Random(Player);
Reply
#3

Quote:
Originally Posted by iggy1
Посмотреть сообщение
Have you got 30 players on your server when testing? The random function in your script will just get a random number 0 - 29, it doesn't check if the player is actually connected. Easiest way i can think of is using foreach.
pawn Код:
new randomplayer = Iter_Random(Player);
hah ok ... so now it must work ?!
Reply
#4

pawn Код:
#include <foreach>//you need this include

new randomplayer = Iter_Random(Player);
TogglePlayerSpectating(playerid, 1);
PlayerSpectateVehicle(playerid,randomplayer);
You will want to do some additional checks to make sure you don't spec yourself ect.
Reply
#5

Well
pawn Код:
r:new randomplayer = random(MAX_PLAYERS); // Instead of 30, put MAX_PLAYERS
if( IsPlayerConnected( randomplayer ) && randomplayer != playerid )
{
    TogglePlayerSpectating(playerid, 1);
    PlayerSpectateVehicle(playerid, GetPlayerVehicleID( randomplayer ) );
}
else goto r;
Checking if it's connected.
Reply
#6

Thats not very nice code though foreach is much better than that in every way. You dont even need to loop. That code is actually extremly inefficient especially if only a few players are connected.
Reply
#7

Quote:
Originally Posted by iggy1
Посмотреть сообщение
pawn Код:
#include <foreach>//you need this include

new randomplayer = Iter_Random(Player);
TogglePlayerSpectating(playerid, 1);
PlayerSpectateVehicle(playerid,randomplayer);
You will want to do some additional checks to make sure you don't spec yourself ect.
No i think i don't need to add some code ...
pawn Код:
public OnPlayerSpawn(playerid)
{
        if(EndRoundCounter != 0)//if the round is busy ..
        {

            SendClientMessage(playerid, COLOR_RED,"Sorry, you can't join while the round is in progress, you have to wait ...");
            SendClientMessage(playerid, COLOR_ORANGE,"While you have to wait, you can specate some players");
            GameTextForPlayer(playerid, "~y~Round in progress ...", 1250, 3);
            for(new i; i<MAX_PLAYERS; i++)
            {
                new randomplayer = Iter_Random(Player);
                TogglePlayerSpectating(playerid, 1);
                PlayerSpectateVehicle(playerid,randomplayer);
            }

        }
        //.....
Im using it under OnPlayerSpawn()
Reply
#8

Yeh, I deleted the loop.
EDIT: Shoudn't that "playerid" in TooglePlayerSpectating be "i" and all the other "playerid"s?
Reply
#9

micheal you dont need that loop you are not even using the var 'i'.
Reply
#10

Quote:
Originally Posted by Mean
Посмотреть сообщение
Yeh, I deleted the loop.
EDIT: Shoudn't that "playerid" in TooglePlayerSpectating be "i" and all the other "playerid"s?
Quote:
Originally Posted by iggy1
Посмотреть сообщение
micheal you dont need that loop you are not even using the var 'i'.
ah lol Xd

So just this:
pawn Код:
new randomplayer = Iter_Random(Player);
                TogglePlayerSpectating(playerid, 1);
                PlayerSpectateVehicle(playerid,randomplayer);
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)