Spectating player system

Started by gamingpro, Mar 28, 2023, 05:44 PM

Previous topic - Next topic

gamingpro

class Spectating
{
isspectating = false;
}
function onScriptLoad(){
spec <- array(GetMaxPlayers(), null);
print("Spectating system has been loaded successfully by gamingpro.");
}
function onPlayerJoin( player ){
spec[player.ID] = Spectating();
}
function onPlayerCommand( player, command, text ){
local cmd;
cmd = command.tolower();
if(cmd == "spec"){
if(player.IsSpawned) ::MessagePlayer("[#FF0000]Error: [#FFFFFF]You must be not spawned before use this command.",player);
else {
if(!text) MessagePlayer("[#FF0000]Error: [#FFFFFF]Correct Syntax /"+cmd+" <player>",player);
else {
local plr = GetPlayer(GetTok(text, " ",1));
if(!plr) ::MessagePlayer("[#FF0000]Error: [#FFFFFF]Unknow player...",player);
else if(plr.Name == player.Name) ::MessagePlayer("[#FF0000]Error: [#FFFFFF]You can't spectating your self!",player);
else if(!plr.IsSpawned) MessagePlayer("[#FF0000]Error: [#FFFFFF]This player isn't spawned yet...",player);
else if(spec[player.ID].isspectating) MessagePlayer("[#FF0000]Error: [#FFFFFF]You're already in spectate, if u want to leave spec use /exitspec",player);
else {
ClientMessage("You're spectating:[ "+plr.Name+" ] Now.",player,0,255,0);
player.SpectateTarget = plr;
spec[player.ID].isspectating = true;
}
}
}
}
else if(cmd == "exitspec"){
if(!spec[player.ID].isspectating) MessagePlayer("[#FF0000]You're not spectating yet...",player);
else {
MessagePlayer("[#00FF00]You've left the spectating.",player);
player.SpectateTarget = false;
}
}
}

function GetTok( string, separator, n, ... )
{
 local m = ( vargv.len() > 0 ) ? vargv[ 0 ] : n, tokenized = split( string, separator ), text = "";

 if ( ( n > tokenized.len() ) || ( n < 1 ) ) return null;

 for ( ; n <= m; n++ )
 {
  text += text == "" ? tokenized[ n - 1 ] : separator + tokenized[ n - 1 ];
 }

 return text;
}
function NumTok(string, separator)
{
    local tokenized = split(string, separator);
    return tokenized.len();
}
function GetPlayer( target )
{
 local target1 = target.tostring();

 if ( IsNum( target ) )
 {
  target = target.tointeger();

  if ( FindPlayer( target) ) return FindPlayer( target );
  else return null;
 }
 else if ( FindPlayer( target ) ) return FindPlayer( target );
 else return null;
}