Method
There are probably multiple ways to go about doing this, some in which are probably better than others. Given no external source to work with (in this scenario) besides what's given to us, this could be done with DataStoreService
.
Setting up the data store
First things first, let's create our data stores so we can access and change this data remotely between servers:
2 | local DataStore = game:GetService( "DataStoreService" ) |
5 | local Servers = DataStore:GetDataStore( "Servers" ) |
Now, the way this will work will be when the server first opens, we should have something at the beginning
of our script to run right away, and only once to let us know that this new server exists. The value we'll be saving will be a table
in dictionary format
, to easily access and erase old information from what our storage is holding.
NetworkServer service
Information regarding the server's identity
and uniqueness
is crucial in saving this information to the right place. Much like how you should use a player's UserId
when saving player data, instead of their username
. For getting said information, we'll be using the NetworkServer
service, and the built-in game
reference (obviously). This should be the format of the information we're saving:
2 | [ GamePortID ] = { JobId = 1 } , |
3 | [ GamePortID ] = { JobId = 2 } , |
4 | [ GamePortID ] = { JobId = 3 } , |
The "GamePortID" is where the NetworkServer
comes in use. Using this service, we can get the Port
ID of the server (and as you can guess, each server has it's own individual port ID, just like how all players have their individual UserIds
). Now let's save this information at the start of our script:
02 | local Network = game:GetService( "NetworkServer" ) |
05 | local DataStore = game:GetService( "DataStoreService" ) |
06 | local Servers = DataStore:GetDataStore( "Servers" ) |
09 | if not Servers:GetAsync( "ActiveServers" ) then |
10 | Servers:SetAsync( "ActiveServers" , { } ) |
14 | Servers:UpdateAsync( "ActiveServers" , function (list) |
15 | list [ Network.Port ] = { |
The code above will now store a dictionary of every server that starts from this point forth. However, this could be dangerous is we don't have a way to remove old, inactive servers.
Removing closed servers
The best way to go about removing a server from the saved list when the game closes, is probably the game.OnClose
callback function. This function runs when the game invokes a shutdown
, but will yield the shutdown until the callback has terminated. Let's implement it:
01 | local Network = game:GetService( "NetworkServer" ) |
02 | local DataStore = game:GetService( "DataStoreService" ) |
03 | local Servers = DataStore:GetDataStore( "Servers" ) |
05 | if not Servers:GetAsync( "ActiveServers" ) then |
06 | Servers:SetAsync( "ActiveServers" , { } ) |
09 | Servers:UpdateAsync( "ActiveServers" , function (list) |
10 | list [ Network.Port ] = { |
17 | game.OnClose = function () |
19 | Servers:UpdateAsync( "ActiveServers" , function (list) |
20 | list [ Network.Port ] = nil |
And now you have an effective way to track your servers activity and get their individual information. I'd give more examples of how to use this, but I'm pretty sure It'd exceed the character limit answers can contain.
Better example and Full demonstration
I'm working on a place where I'll implement everything I talked about here, and update my answer once it's done. I'll make it open sourced so you can see just exactly how to use everything I talked about.
Questions
If you have any further questions after that, just let me know and I'll get back to it as soon as possible. Hope you found what you were looking for!
Locked by ScriptGuider
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?