SA-MP Forums Archive
Setting a player to spectate his team - 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: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Setting a player to spectate his team (/showthread.php?tid=135372)



Setting a player to spectate his team - biltong - 20.03.2010

Ok so I have this code that runs 2 seconds after a player dies:
pawn Код:
public SetPlayerSpectating(playerid)
{
  TogglePlayerSpectating(playerid,1);
    new randomplayer = random(MAX_PLAYERS);
    if(IsPlayerConnected(randomplayer))
    {
      if(IsPlayerDead[randomplayer] == 0)
        {
          if(Team[randomplayer] == Team[playerid])
          {
            PlayerSpectatePlayer(playerid,randomplayer,1);
                new name[MAX_PLAYER_NAME];
                GetPlayerName(randomplayer,name,sizeof(name));
                new string[64];
                format(string,sizeof(string),"Spectating: %s",name);
                GameTextForPlayer(playerid,string,5000,5);
                return 1;
            }
            else SetPlayerSpectating(playerid);
        }
        else SetPlayerSpectating(playerid);
    }
    else SetPlayerSpectating(playerid);
    return 1;
}
I have 2 issues with that.
1) If the player is the only one on his team, he spectates his body flying upwards at an insanely high speed for some reason.
2) The player can just press the spacebar to stop spectating and respawn, which I don't want.

How can I fix this?


Re: Setting a player to spectate his team - XGh0stz - 20.03.2010

All those else's you have at the bottom mean if there isn't anyone else to watch, you're gonna watch yourself & because you're not spawned is probably where all those wierd effects are coming from...

To stop a player from re-spawning, you could try TogglePlayerControllable(playerid,1); but not positive that would work. If not, create a variable and check this variable for OnPlayerSpawn, if the player is not meant to be spawned, then throw in the alt. code to keep them from spawning etc.


Re: Setting a player to spectate his team - biltong - 20.03.2010

Hmmmm.... Maybe I should just ditch the spectating idea and make the player spawn somewhere away from where they normally spawn.


Re: Setting a player to spectate his team - XGh0stz - 20.03.2010

Well spectating or whatever is not something I've come across yet, maybe someone else would be able to give greater advice


Re: Setting a player to spectate his team - MadeMan - 20.03.2010

Try this:

pawn Код:
PlayerSpectatePlayer(playerid,randomplayer);
without 1 at the end.


Re: Setting a player to spectate his team - MadeMan - 20.03.2010

Anyway I suggest using this code:

pawn Код:
public SetPlayerSpectating(playerid)
{
    new PlayerList[MAX_PLAYERS], count;
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) && IsPlayerDead[i] == 0 && Team[i] == Team[playerid])
        {
            PlayerList[count] = i;
            count++;
        }
    }
    new randomplayer = PlayerList[random(count)];
    TogglePlayerSpectating(playerid,1);
    PlayerSpectatePlayer(playerid,randomplayer);
    new name[MAX_PLAYER_NAME];
    GetPlayerName(randomplayer,name,sizeof(name));
    new string[64];
    format(string,sizeof(string),"Spectating: %s",name);
    GameTextForPlayer(playerid,string,5000,5);
    return 1;
}



Re: Setting a player to spectate his team - biltong - 20.03.2010

I tried using this:
pawn Код:
public SetPlayerSpectating(playerid)
{
  TogglePlayerSpectating(playerid,1);
    new randomplayer = random(MAX_PLAYERS);
    if(IsPlayerConnected(randomplayer))
    {
      if(!IsTeamDead(GetPlayerTeam(playerid))) //makes sure the player's team is dead, else call end of round
      {
        if(IsPlayerDead[randomplayer] == 0) //checks if the random player is dead or not
            {
              if(Team[randomplayer] == Team[playerid]) //makes sure the player and the random player are on the same team
              {
                PlayerSpectatePlayer(playerid,randomplayer,1); //set the player spectating
                    new name[MAX_PLAYER_NAME];
                    GetPlayerName(randomplayer,name,sizeof(name));
                    new string[64];
                    format(string,sizeof(string),"Spectating: ~y~%s",name);
                    GameTextForPlayer(playerid,string,5000,5);
                    TogglePlayerControllable(playerid,false); //stops the player from exiting spectator mode
                    new playername[MAX_PLAYER_NAME];
                    GetPlayerName(playerid,playername,sizeof(playername));
                    printf("[SERVER]%s is now spectating %s.",playername,name);
                    return 1;
                }
                else SetPlayerSpectating(playerid); //try for new random player
            }
            else SetPlayerSpectating(playerid); //try for new random player
        }
        else
        {
          KillTimer(RoundsTimer); //stops the current timer that ends the round
            RoundTimer(); //skips to end of round
        }
    }
    else SetPlayerSpectating(playerid); //try for new random player
    return 1;
}
That still didn't work even if another player was on the dying player's team, I specc'd them for 2 seconds then all of a sudden BAM spectating a flying body.
Quote:
Originally Posted by MadeMan
Anyway I suggest using this code:

pawn Код:
public SetPlayerSpectating(playerid)
{
    new PlayerList[MAX_PLAYERS], count;
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) && IsPlayerDead[i] == 0 && Team[i] == Team[playerid])
        {
            PlayerList[count] = i;
            count++;
        }
    }
    new randomplayer = PlayerList[random(count)];
    TogglePlayerSpectating(playerid,1);
    PlayerSpectatePlayer(playerid,randomplayer);
    new name[MAX_PLAYER_NAME];
    GetPlayerName(randomplayer,name,sizeof(name));
    new string[64];
    format(string,sizeof(string),"Spectating: %s",name);
    GameTextForPlayer(playerid,string,5000,5);
    return 1;
}
I shall try it

EDIT: MadeMan I tried your code but it had the same result as my new code: spectate for 2 seconds then BAM flying body.

Sigh, that means I have more bugs to squash.


Re: Setting a player to spectate his team - MadeMan - 20.03.2010

Yes, the mistake is somewhere else.


Re: Setting a player to spectate his team - biltong - 21.03.2010

Well I fixed the flying body bug, it seems if you set the player to spectate when they die, they respawn while spectating causing the bug, while if you check if the player died in OnPlayerSpawn and THEN set him to spectate, you get no issues.

Now I just need to work out how to stop them from pressing the spacebar to get out of spectating.