Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Is there a function for getting a list of servers in your game? [closed]

Asked by
Klamman 220 Moderation Voter
8 years ago

I was at Apocalypse Rising the other day, and I noticed a system they had in place that was really cool. They have a list of available servers where you can press join to join a server with a specified amount of people. Additionally, you can follow friends in the GUI. How would that be achieved? Is there a function or method for getting a list of servers, or seeing what server a friend is in? Thanks in advance.

1
No, but you could use DataStores to send and retrieve data between servers. TheDeadlyPanther 2460 — 8y
1
I'm pretty sure when a player decides to create a server they press a button and that stores data globally. UniversalDreams 205 — 8y
0
There actually is an API for this, but I don't know the link to it Kurieita 125 — 8y

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?

1 answer

Log in to vote
9
Answered by 8 years ago
Edited 7 years ago

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:

-- DataStoreService
local DataStore = game:GetService("DataStoreService")

-- Creating (or accessing) a data store called "Servers"
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:

Information = {
    [GamePortID] = {JobId = 1},
    [GamePortID] = {JobId = 2},
    [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:

-- Get the NetworkServer service
local Network = game:GetService("NetworkServer")

-- Data
local DataStore = game:GetService("DataStoreService")
local Servers = DataStore:GetDataStore("Servers")

-- Let's also assign our data store key a value in case this is the first time it's saving
if not Servers:GetAsync("ActiveServers") then
    Servers:SetAsync("ActiveServers",{}) -- Save an empty table to it
end

-- Here I'm using the key "ActiveServers" to represent the dictionary of servers we'll be working with. "list" is the current value saved to the key, as you should know if you know how UpdateAsync works.
Servers:UpdateAsync("ActiveServers",function(list)
    list[Network.Port] = { -- index is now the Port id of the game

        -- Here we're just saving some information that may or may not be useful depending on what you're using this for.
        RunTime = os.time(), -- time the game started
        JobId = game.JobId, -- individual game's job id
    }
end)

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:

local Network = game:GetService("NetworkServer")
local DataStore = game:GetService("DataStoreService")
local Servers = DataStore:GetDataStore("Servers")

if not Servers:GetAsync("ActiveServers") then
    Servers:SetAsync("ActiveServers",{})
end

Servers:UpdateAsync("ActiveServers",function(list)
    list[Network.Port] = {
        RunTime = os.time(), -- time the game started
        JobId = game.JobId, -- individual game's job id
    }
end)

-- The callback function when the game closes
game.OnClose = function()
    -- Update the old data
    Servers:UpdateAsync("ActiveServers",function(list)
        list[Network.Port] = nil -- removes old information
        return list
    end)
    wait(5) -- waits 5 seconds before shutdown. this is an amount of time I'm usually comfortable with.
end

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!

Ad