Cycle command through connected players

CorrosionX

Plugin Developer
Plugin Developer
Sep 3, 2014
212
86
18
California
What is the easiest way to run a simple command on all currently connected? I have tried several for/foreach on Server.Players but never could get it working right. Any help?
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,095
4,815
113
At your house.
github.com
What is the easiest way to run a simple command on all currently connected? I have tried several for/foreach on Server.Players but never could get it working right. Any help?
You mean something like this?

JavaScript:
function On_Command(Player, cmd, args) {
    if (cmd == "executenyancat" && Player.Admin) {
        ExecuteNyanCats();
    }
}

function ExecuteNyanCats() {
    for (var cplayer in Server.Players) {
        cplayer.SendCommand("grass.on true");
        cplayer.Message("You just got nyantetised!");
    }
}
 

CorrosionX

Plugin Developer
Plugin Developer
Sep 3, 2014
212
86
18
California
You mean something like this?
This is my current version...

JavaScript:
//UnlimitedIteminHand.
//Author: CorrosionX
function On_PluginInit(){
   Plugin.CreateTimer("GiveUsesTimer", 1).Start();
}
function GiveUsesTimerCallback(){
   for(var player in Server.Players)
   {
     if(player != null && player.Inventory.InternalInventory.activeItem != null)
     {
       player.Inventory.InternalInventory.activeItem.SetUses(250);
     }
   }
   Server.Broadcast("Timer Completed");
   Plugin.CreateTimer("GiveUsesTimer", 10000).Start();
}
tried that had issues...1 of the many iterations of code i tried.

BTW dont use 'while' .....to execute commands on players XD
 
Last edited:

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,095
4,815
113
At your house.
github.com
This is my current version...

JavaScript:
//UnlimitedIteminHand.
//Author: CorrosionX
function On_PluginInit(){
   Plugin.CreateTimer("GiveUsesTimer", 1).Start();
}
function GiveUsesTimerCallback(){
   for(var player in Server.Players)
   {
     if(player != null && player.Inventory.InternalInventory.activeItem != null)
     {
       player.Inventory.InternalInventory.activeItem.SetUses(250);
     }
   }
   Server.Broadcast("Timer Completed");
   Plugin.CreateTimer("GiveUsesTimer", 10000).Start();
}
tried that had issues...1 of the many iterations of code i tried.

BTW dont use 'while' .....to execute commands on players XD
What do you mean about while?

You problem is simple. You are trying to create a timer that is already existing.
And that 1 can even kill ur timer.
Before the for cycle add a Plugin.KillTimer("GiveUsesTimer");


Also IDK why do you want to add durability uses, since you can set the durability off via server.cfg.
 

mikec

Master Of All That I Survey
Retired Staff
Trusted Member
Jul 12, 2014
296
152
28
Los Angeles, California, USA
What is the easiest way to run a simple command on all currently connected?
JavaScript:
function SetUses(player) {
    if(player != null && player.Inventory.InternalInventory.activeItem != null) }
        player.Inventory.InternalInventory.activeItem.SetUses(250);
    }
}
Server.Players.ForEach(SetUses);
Although, this is how I would do it:
JavaScript:
function SetUses(player) {
    try {
        player.Inventory.InternalInventory.activeItem.SetUses(250);
    } catch(ignore) {}
}
Server.Players.ForEach(SetUses);
 

CorrosionX

Plugin Developer
Plugin Developer
Sep 3, 2014
212
86
18
California
What do you mean about while?

You problem is simple. You are trying to create a timer that is already existing.
And that 1 can even kill ur timer.
Before the for cycle add a Plugin.KillTimer("GiveUsesTimer");


Also IDK why do you want to add durability uses, since you can set the durability off via server.cfg.
When you set uses to items in hand, it sets the CLIP of the weapon and AMOUNT you have to the SetUses(#). Accidently stumbled on it XD going to be for the unlimitedAmmo plugin I wanted to do. So basically with an weapon in hand and this running, you can fire forever as it sets clip to max amount with timer. It's awesome XD


This is why dont use while and setting command on player..:
JavaScript:
function On_PlayerSpawned(Player){
      while(Player.Health > 0 && Player != null){
           if(player.Inventory.InternalInventory.activeItem != null){
                 player.Inventory.InternalInventory.activeItem.SetUses(250);
          }
     }
}
Caused server to lag out when connected XD

JavaScript:
function SetUses(player) {
    if(player != null && player.Inventory.InternalInventory.activeItem != null) }
        player.Inventory.InternalInventory.activeItem.SetUses(250);
    }
}
Server.Players.ForEach(SetUses);
Tried this too...had problems for some odd reason. Probably was my timer ALL along. Stupid me.
 
Last edited:

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,095
4,815
113
At your house.
github.com
When you set uses to items in hand, it sets the CLIP of the weapon and AMOUNT you have to the SetUses(#). Accidently stumbled on it XD going to be for the unlimitedAmmo plugin I wanted to do. So basically with an weapon in hand and this running, you can fire forever as it sets clip to max amount with timer. It's awesome XD


This is why dont use while and setting command on player..:
JavaScript:
function On_PlayerSpawned(Player){
      while(Player.Health > 0 && Player != null){
           if(player.Inventory.InternalInventory.activeItem != null){
                 player.Inventory.InternalInventory.activeItem.SetUses(250);
          }
     }
}
Caused server to lag out when connected XD


Tried this too...had problems for some odd reason
I never ever said to use "While" at anywhere. O_O
 

CorrosionX

Plugin Developer
Plugin Developer
Sep 3, 2014
212
86
18
California
I never ever said to use "While" at anywhere. O_O
I know, but I had tried everything else but kill timer, so I know what I was getting into, but tried "While" anyways. Log file was 500MB+ couldn't even open it with notepad++. lol
 
Last edited:

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,095
4,815
113
At your house.
github.com
I know, but I had tried everything else but kill timer, so I know what I was getting into, but tried "While" aways. Log file was 500MB+ couldn't even open it with notepad++. lol
Well all i can see is that when the timer runs the void, you dont stop it, but you are trying to recreate it, while the timer is still running.
 

mikec

Master Of All That I Survey
Retired Staff
Trusted Member
Jul 12, 2014
296
152
28
Los Angeles, California, USA
Tried this too...had problems for some odd reason.
It's not odd. It's weirdness that results from trying to use statically typed objects from a dynamically typed language. It should work if you use pass an anonymous function to ForEach. I know Magma gets it right when you do that. Jint does too, But it's hit or miss whether a named function will work. ;)

JavaScript:
Server.Players.ForEach(
        function(player) {
            try{
                player.Inventory.InternalInventory.activeItem.SetUses(250);
            } catch(ignore) { }
        }
    );