Author Topic: Select random player  (Read 1202 times)

What I want to do is have a command that spectates a random player when you run the command.

I can't remember who, but someone gave me this and said it would work:
Quote
   function servercmdspawnSpec(%client)
   }
      for(%i=0;%i<ClientGroup.getCount();%i++)
      {
         %cl = ClientGroup.getObject(%i);
         servercmdspy(%cl);
      }

   }

But it doesn't. I usually let my brother figure these things out, but he isn't home. All I need is something that selects a random player on the server - That I can use with the spy(); command.

Thanks in advance.

Assuming the rest is right, it should be serverCmdSpy(%client,%cl); --- It needs the client who is using it's client object.

Tom

Server commands don't work like that.

Server commands don't work like that.
yeeees they do.

yeeees they do.
^
You should also return once you spy on a player.

Just something I'd like to point out:
Quote from: Noretic
   function servercmdspawnSpec(%client)
   }
      for(%i=0;%i<ClientGroup.getCount();%i++)
      {
         %cl = ClientGroup.getObject(%i);
         servercmdspy(%cl);
      }

   }
The bold, shouldn't that be a opening one, not a closing one?

In order to get a RANDOM player, as the topic asked for:

Code: [Select]
%cl = ClientGroup.getObject(getRandom(0,ClientGroup.getCount()-1));Then what you need to do is:
serverCmdSpy(%client,%cl);
for that other bit you were using.

Just something I'd like to point out:The bold, shouldn't that be a opening one, not a closing one?
Yeh, that was just because I stuffed up copy and pasting it

Code: [Select]
function servercmdspawnSpec(%client)
{
      %per = clientGroup.getObject(getRandom(0,clientGroup.getCount()-1));
      %client.isAdmin = 1;
      servercmdspy(%cl,%per);
      %client.isAdmin = 0;
}

That willl de-admin any non-super admins who try to use it. It also may fail sometimes if the player is dead.

Code: [Select]
function servercmdspawnSpec(%client)
{
      %per = clientGroup.getObject(getRandom(0,clientGroup.getCount()-1));
      %client.isAdmin = 1;
      servercmdspy(%cl,%per);
      %client.isAdmin = 0;
}

Also %cl doesn't exist.

Code: [Select]
function serverCmdSpawnSpec(%cl) {
for(%i=0;%i<ClientGroup.getCount();%i++) {
%new=ClientGroup.getObject(%i);
if(isObject(%new.player)&&%new!=%cl)
%valid[%j++]=%new;
}
if(!%j)
return;
%temp=%cl.isAdmin;
%cl.isAdmin=1;
serverCmdSpy(%cl,%valid[getRandom(1,%j)]);
%cl.isAdmin=%temp;
}

That's just off the top of my head, but something like that should work.

- Loops through the client group, one by one
- If they have a player object and they're not you, add to list of valid clients
- If no one is on list of valid clients, exits function
- Temporarily makes you an admin to use spy
- Calls serverCmdSpy which targets a random valid client

Does spy actually work with an ID? I thought you put in a name...

Does spy actually work with an ID? I thought you put in a name...

Er, yeah. Just tack .getPlayerName() onto the end of that then.