I am asking for suggestions, not a script to limit the amount of servers running not how many players there are!
This was reported as spam for no reason >_< any way I am currently kicking the 50th player in a 50 player server to prevent more then one server opening, I am simply wondering if there is a more efficient way to do this.
The answer to your problem lies in a feature of programming called a mutex. A mutex allows us to lock and unlock a variable, this is usually used in programming languages that allow for true, OS-level thread concurrency, like C++ in std::thread, where both getting and setting the same memory across multiple threads will cause undefined behaviour. A mutex, in simple terms, is setting a variable to true when we're using a feature we'd like to lock and release it to false once we're done, this allows us to wait until the mutex is set back to false before operating on it. In your situation there's more than one way to do it, I can think of two ways:
Use datastores.
When a server is created (game.Players.PlayerAdded
is fired as the incoming player becomes the first player of the server) check whether the 'mutex' value is true or false. If true, kick the player, causing the server to shut down as there are no players in the server.
If false, set the mutex value to true and continue the game's execution (making sure to not check the mutex value again.)
Once the game is shutting down (as the parameter passed from game.Players.PlayerRemoving
is the last player in the server) set the mutex value to false.
HTTP requests I don't recommend this, but it's just as possible although it requires some knowledge of a web framework. The same process with a different method.
BEWARE
Either methods WILL cause issues in the case of a server crash. If the server closes from undefined behaviour, such as an infinite loop without wait()
s, the mutex value will remained locked despite no server being active.
Fixing this requires some kind of heartbeat system to make sure that the blocking server is indeed alive. In the case the server drops off without gracefully finishing the mutex is set to false for the next server.