This server list code isnt working

-- Let's ask neighbouring servers for their server data.
game:GetService('ReplicatedStorage').GetServerList.OnServerInvoke = function(Player)
    local Servers = {}
    MessagingService:PublishAsync(GetServerRequest)
    task.spawn(function()
        local ServerData = game:GetService('ReplicatedStorage').ReturnServerData.Event:Connect(function(Data)
            Data = Data.Data
            Servers[#Servers+1] = Data -- This returns a value
            warn(Servers, ' start')
        end)
    end)
    warn(Servers, ' end')
    task.wait(3)
    return Servers
end

A friend needs some help figuring out why this code errors
image

I think it’s because he is testing in studio, I’m probably wrong.

1 Like

i would do something like this for a server list

local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("Servers")
local serverList = {}
local updateRate = 1

local function UpdateServerList()
    local newServerList = {}
    local exclusiveLowerBound = nil
    while true do
        local success, value = pcall(sortedMap.GetRangeAsync, sortedMap, Enum.SortDirection.Ascending, 200, exclusiveLowerBound)
        if success == false then return end
        for i, data in ipairs(value) do newServerList[data.key] = data.value end
        if #items < 200 then break end
        exclusiveLowerBound = value[#value].key
    end
    serverList = newServerList
end

local function Loop()
    task.delay(updateRate, Loop)
    local success, value = pcall(sortedMap.SetAsync, sortedMap, game.JobId, #game.Players:GetPlayers(), updateRate + 1)
    UpdateServerList()
    print(serverList)
end

Loop()
2 Likes