You have the right idea, but sadly you can't broadcast the array type directly via publicVariable. I think, from your code, you are also assuming that publicVariable makes a variable global across all machines, when what it actually does is copy its current contents across all machines
once. If you want to keep it updated, then you have to publicVariable it every time it is altered (but obviously not in this case, since you can't publicVariable an array, as I said).
My solution to your problem involves some relatively advanced methods (Well, if you aren't used to MP scripting), so I am not sure I can explain it fully, but:
// List of players should be initalised on all machines.
listofplayers = [];
// The server requests the connecting player name be added to the list, or the disconnecting player name be removed from the list:
if (isServer) then
{
OnPlayerConnected "global_command = format [""listofplayers = listofplayers + [""""%1""""]"", _name]; publicVariable ""global_command"";";
OnPlayerDisconnected "global_command = format [""listofplayers = listofplayers - [""""%1""""]"", _name]; publicVariable ""global_command"";";
};
// global_command is used to perform remote command execution.
global_command = "";
// Monitor global_command for new commands and then execute them.
[] spawn
{
while {true} do
{
waitUntil {global_command != ""};
call compile global_command;
global_command = "";
};
};
In the script above, I've used a general method for executing code on remote machines, which isn't specific to the list of players. You might find other uses for it in MP games. Note that though I've given it a quick check for syntax errors, I haven't tested it in a proper MP game.
** EDIT **
As usual with MP scripting, I overlooked something. Both Mandoble's and my version will record a "player" called __server__ which isn't a real player. You can just ignore that player in the list or prevent the player name being added if it is __server__ (or __SERVER__, I can't remember which).
The other issue is that my version isn't JIP-compatible since only the server and those players in the game from the start will have the correct list (Mandoble's is fine for JIP since it only works on the server anyway). JIP players will not have any names of people who joined the game before them, so If you need JIP players to have the correct list, then I'll have to do things differently...oops! MP scripting is never straightforward