Help with DataStore/Serverlist

How would I make a server list/ server list UI?

I’ve tried making it with datastores like this:

local serverList = game:GetService("DataStoreService"):GetDataStore("ServerList")

if not game:GetService("RunService"):IsStudio() then
    if serverList:GetAsync("ServerList") == nil then
        serverList:SetAsync("ServerList", {})
    end

    local list = serverList:GetAsync("ServerList")
    
    if not list[game.JobId] then
        local list = serverList:GetAsync("ServerList")
        table.insert(list, game.JobId)
        serverList:SetAsync("ServerList", list)
        print("Hey")
    end
    
    game.OnClose:Connect(function()
        if list[game.JobId] then
            table.remove(serverList:GetAsync("ServerList"), game.JobId)
            serverList:SetAsync("ServerList", list)
            print("Hey")
        end
    end)
    
    serverList:OnUpdate("ServerList", function()
        print("Server!")
        
        for _, v in pairs(serverList:GetAsync("ServerList")) do
            print(v)
        end
    end)
end

But it doesn’t work because “Argument 1 missing or nil” on line 4 which I don’t know why.

Your setting an empty Table in the SetAsync, this is considered a nil value. If your not setting any data in the DataStore, then there is no point having the SetAsync function in your code.

Your preforming a function which sets the data which you’ve done with an empty table. the data expected back would be empty as you then try to retrieve that data you set few lines foward. I recommend you also to wrap your datastore functions in a pcall() to handle things that could possibly error. Here is an example from my datastore system below!

local saveData

local success, err	= pcall(function()
	saveData	= serverList:SetAsync( -- Your key -- , -- Data-- )
end)

if err then
	warn(err)
end

if success then
	if saveData then	
	end
end