Jump to content

Execute Code When X Players Are Online


FuelLF

Recommended Posts

I'm sure this will be useful to some people. The script will execute any code or file you want it to when a certain amount of players online have been reached.

 

Install Instructions:

Server Install: (Not Tested)

Spoiler

Navigate to your @DayZ_Epoch or Overpoch Server files Then AddOns,

Unpack DayZ_Server.pbo with PBOManager.

Paste the File FuelLF into the main directory of your server

Goto System\Server_Monitor.sqf (Find ) AllowConnection = true;

Past the following code above it. If you have any other scripts installed paste this below them!

execVM "\z\addons\dayz_server\FuelLF\units.sqf";

You Are Done! Repack the PBO and start your server!

 

Clientside Install: (Recommended)

Spoiler

Navigate to your Instance init.sqf (usually at server/mpmissions/init.sqf)

Paste The File Included in the .Zip folder into your mission file.

Open Init.sqf and at the VERY bottom paste the following code:

execVM "FuelLF\units.sqf";

 

Download: https://www.mediafire.com/?3l0my14qtk1e9ye

 

By Default this script will not do anything.

Go into FuelLF folder and units.sqf

There are further instructions inside the file.

Link to comment
Share on other sites

This "script" will run once and then it's done. So the player amount that you want to have has to be on the server when this script gets executed. I don't think that's what you want.

I guess this is made for running something when X players are connected, so you need to make a loop instead of an if condition.

//Player online loop
while (true) do {
	if (count playableUnits == 1) then {
		something;
	};

	sleep 5;
};

Something like this is more recommended because this will run forever each 5 seconds (that's what the sleep is for, also it prevents lag). And when 1 player is connected (at least in this example) then this something code gets executed. Also I would add something like a fulfilled variable so the code doesn't get executed whenever the loop is running. So like:

//Player online loop
_done = false;

while (true) do {
	if ((count playableUnits == 1) && !_done) then {
		something;
		_done = true;
	};

	sleep 5;
};

So when this code in the if condition is executed once, it will never execute this code again because _done is set to true.
And by the way, you should place a ";" when you close a codeblock so after each "}".

Link to comment
Share on other sites

3 hours ago, juandayz said:

 

very nice! mate! 

you can reduce all this line with this: (do not take it as a correction, just another way to do it :smile:)


_player_amount = count playableunits;
_amount_value = 10;//player amount
waitUntil{_player_amount >=_amount_value};
//execute the code

 

Also a way to do it, yeah. :)
As always, there are many ways to get what you want.

Link to comment
Share on other sites

4 hours ago, juandayz said:

 

very nice! mate! 

you can reduce all this line with this: (do not take it as a correction, just another way to do it :smile:)


_player_amount = count playableunits;
_amount_value = 10;//player amount
waitUntil{_player_amount >=_amount_value};
//execute the code

 

I would recommend using this method, but also making sure to add a uiSleep, as a continuous loop will cause some lag. You also cannot define the count of playable units before the waitUntil, as it sets the variable to a number, therefore the value will never be true as the amount will always show as the number of players that were on when the variable was set.

 

The correct way would be:

private "_neededAmount";

_neededAmount = 10;
waitUntil {uiSleep 5; count playableUnits > _neededAmount};

 

Link to comment
Share on other sites

19 hours ago, DAmNRelentless said:

-snip-

This script was intended to do two things:

1. Either run once (Whenever you Execute it, Like in a certain function)

2. or When something happens on the server Exec this script. You could do a loop but for example if you had it Hint to all players you would be spammed with the message every time the script loops. (Unless you add a bool)

Link to comment
Share on other sites

43 minutes ago, FuelLF said:

This script was intended to do two things:

1. Either run once (Whenever you Execute it, Like in a certain function)

2. or When something happens on the server Exec this script. You could do a loop but for example if you had it Hint to all players you would be spammed with the message every time the script loops. (Unless you add a bool)

That's what I just said

Link to comment
Share on other sites

  • 2 months later...
11 minutes ago, totis said:

Hi,

I was wondering if i could use this script to start a dzai vehicle patrol (the one dam updated) but my knowledge is very limited..

Can you give me an example how to do that?

 

If you want that vehicle patrols only spawn with a specific minimum player amount do the following. Open "dayz_server\DZAI\DZAI\compile\veh_randompatrol.sqf" and replace the whole file with this code:

private ["_unitGroup","_tooClose","_wpSelect"];

waitUntil {
	uiSleep 5;
	count playableUnits > 9;
};

_unitGroup = _this select 0;

_tooClose = true;
while {_tooClose} do {
	_wpSelect = (DZAI_locationsLand call BIS_fnc_selectRandom2) select 1;
	if (((waypointPosition [_unitGroup,0]) distance _wpSelect) > 300) then {
		_tooClose = false;
	};
};

_wpSelect = [_wpSelect,random(300),random(360),0,[1,300]] call SHK_pos;
[_unitGroup,0] setWPPos _wpSelect; 
[_unitGroup,0] setWaypointCompletionRadius 150;
if ((count (waypoints _unitGroup)) == 1) then {
	_unitGroup setCurrentWaypoint [_unitGroup,0];
};

I set the needed player amount to 9. If you want to change that you have to edit this line:

count playableUnits > 9;

 

Link to comment
Share on other sites

Tnx Dam.

But i see now was fast to type it and i was wrong as to what i wanted exactly.

Im using land patrols and i want those to spawn from start. And i want a jet to spawn lets ay after 9 ppl join.

The code above will make all aspects of patrols (land, air and on foot) to spawn after 9 ppl join am i correct?

Can i alter it so that will point only to air patrols?

Oh wait if i can aplly this to heli_randompatrols.sqf??

 

Link to comment
Share on other sites

Like this between private and _tooclose  ( i used 10 ppl ) :

private ["_unitGroup","_tooClose","_wpSelect"];
_unitGroup = _this select 0;

waitUntil {
    uiSleep 5;
    count playableUnits > 10;
};

_tooClose = true;
while {_tooClose} do {

Do you think there is any code for announcing the spawn of the jet to players?

Link to comment
Share on other sites

Just now, totis said:

Like this between private and _tooclose  ( i used 10 ppl ) :

private ["_unitGroup","_tooClose","_wpSelect"];
_unitGroup = _this select 0;

waitUntil {
    uiSleep 5;
    count playableUnits > 10;
};

_tooClose = true;
while {_tooClose} do {

Do you think there is any code for announcing the spawn of the jet to players?

[nil,nil,rTitleText,"Jet spawn", "PLAIN",10] call RE;

but put it out of the loop.

Link to comment
Share on other sites

On 29. 6. 2017 at 5:11 AM, BigEgg said:

I would recommend using this method, but also making sure to add a uiSleep, as a continuous loop will cause some lag. You also cannot define the count of playable units before the waitUntil, as it sets the variable to a number, therefore the value will never be true as the amount will always show as the number of players that were on when the variable was set.

 

The correct way would be:


private "_neededAmount";

_neededAmount = 10;
waitUntil {uiSleep 5; count playableUnits > _neededAmount};

 

 

Apology... I was commenting older stuff, not in current context...

Edited by iben
Not related to actual context :)
Link to comment
Share on other sites

hmm it doesnt work on my test server. whatever number i put there jet spawns from start and no msg is send.

i have it like this (i put one just to test) :

 

private ["_unitGroup","_tooClose","_wpSelect"];
waitUntil {
    uiSleep 5;
    count playableUnits > 1;
};

_unitGroup = _this select 0;

_tooClose = true;
while {_tooClose} do {
    _wpSelect = (DZAI_locations call BIS_fnc_selectRandom2) select 1;
    if (((waypointPosition [_unitGroup,0]) distance _wpSelect) > 300) then {
        _tooClose = false;
    } else {
        uiSleep 0.1;
    };
};
_wpSelect = [_wpSelect,50+(random 900),(random 360),1] call SHK_pos;
[_unitGroup,0] setWPPos _wpSelect;
[_unitGroup,1] setWPPos _wpSelect;
if ((waypointType [_unitGroup,1]) == "MOVE") then {
    if (0.275 call DZAI_chance) then {
        [_unitGroup,1] setWaypointType "SAD";
        [_unitGroup,1] setWaypointTimeout [20,25,30];
        _unitGroup setVariable ["DetectPlayersWide",true];
    };
} else {
    [_unitGroup,1] setWaypointType "MOVE";
    [_unitGroup,1] setWaypointTimeout [3,6,9];
};
//[_unitGroup,0] setWaypointCompletionRadius 150;
//_unitGroup setCurrentWaypoint [_unitGroup,0];
//(vehicle (leader _unitGroup)) flyInHeight (100 + (random 40));

_unitGroup setCurrentWaypoint [_unitGroup,0];
(vehicle (leader _unitGroup)) flyInHeight (100 + (random 40));

[nil,nil,rTitleText,"WARNING! AN ENEMY A10 HAS SPAWNED!", "PLAIN",10] call RE;

Link to comment
Share on other sites

I am pretty sure that the AI's counts as playableUnit too.

Do something like this to get rid off playableUnits that are actually no players:

_players = playableUnits;
{
	if (!isPlayer _x) then {
		_players = _players - [_x];
	};
} forEach _players;

waitUntil {
	uiSleep 5;
	count _players > 1;
};

Edit: here's the reference for playableUnits and it also includes AI so you should use that in order to prevent AI's from being counted as playableUnits.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...