Refreshing servers (server-list)

I can’t figure out a way to refresh servers, i thought about deleting everything & doing a messaging service check again for each server, but if there are too many servers this can error. Can anyone help me and give me a good method to refresh servers?

--!strict

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Servers: Folder = ReplicatedStorage.Servers

local REFRESH_TIME: number = 5 -- test

local function GetServerType(): string
	if game.PrivateServerId ~= "" then
		if game.PrivateServerOwnerId ~= 0 then
			return "VIPServer"
		else
			return "ReservedServer"
		end
	else
		return "StandardServer"
	end
end

MessagingService:SubscribeAsync("RequestServerInfo", function(Message): ()
	local JobId: string & number, PlayersCount: number = unpack(Message.Data)

	local Server = Instance.new("StringValue")
	Server.Name = if game.JobId == JobId then "Your_Server" else "Server_"..#Servers:GetChildren() + 1
	Server.Value = JobId.."|"..PlayersCount
	Server.Parent = Servers
end)

if GetServerType() == "StandardServer" then
	MessagingService:PublishAsync("RequestServerInfo", {game.JobId, #Players:GetPlayers()})
end
1 Like