Creating a server list using message service

I need help detecting if a server is no longer available and deleting the text-button specified to that place, the folder with the server data already gets deleted once the server is gone but I can’t seem to find a way to send this information to the function and remove the text button.

(PS its my first time using message service/making a server list)


local TweenService = game:GetService("TweenService")

local activeServers = {}

local function refreshServer()
	
	local serverFrames = {}
	for i,v in pairs(game.ReplicatedStorage.Servers:GetChildren()) do
		if script.Parent.Frame:FindFirstChild(v.Name) and game.ReplicatedStorage.Servers:FindFirstChild(v.Name) then
			warn("Already a server")
			return
		end
		warn(v.Name)
		local Template = script.Template:Clone()
		local id = v.Name
		Template.Name = v.Name
		Template.Parent = script.Parent.Frame
		Template:WaitForChild("PlayerCount").Text = v:WaitForChild("Players").Value.."/"..v:WaitForChild("MaxPlayers").Value
		Template:WaitForChild("ServerID").Text = v.Name
		table.insert(serverFrames,Template)		
		Template.MouseEnter:Connect(function()
			TweenService:Create(Template.UIStroke, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {
				Color = Color3.fromRGB(255,255,255);
			}):Play()
		end)
		
		Template.MouseLeave:Connect(function()
			TweenService:Create(Template.UIStroke, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {
				Color = Color3.fromRGB(0, 0, 0);
			}):Play()
		end)
		
		Template.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.Requests.Teleport:FireServer(id)
		end)
		
	end
end

refreshServer()

game.ReplicatedStorage.Servers.ChildAdded:Connect(refreshServer)
game.ReplicatedStorage.Servers.ChildRemoved:Connect(refreshServer)
1 Like

if you mean there are no players then you could use an if statement with #game.Players:GetChildren()
can you explain what you are trying to do here?

I have the refresh function connected to a Child Added function inside a folder that contains the information about all the servers, once a server is no longer available (for example all players left) the information about that server gets deleted and the function is fired again and this is where I want it to delete a text label connected to that server.

I would also like to know if there is a better way to handle Subscribe Async because currently it deletes the server data every 5 seconds, and can be kinda buggy to work with.

local MessageService = game:GetService("MessagingService");
local Servers = game.ReplicatedStorage:FindFirstChild("Servers");

MessageService:SubscribeAsync("ServerInfo",function(Info)
	
	Info = Info.Data
	local id, players, maxplayers
	local ServerFile = nil;
	if Info.ServerId ~= game.JobId and not Servers:FindFirstChild(Info.ServerId) then
		
		id = Info.ServerId
		players = Info.Players
		maxplayers = Info.MaxPlayers
		
		print("Server ID; ", id)
		print("Current Players; ", players)
		print("Max Players; ", maxplayers)
		
		ServerFile = script.Folder:Clone()
		ServerFile.Name = id
		ServerFile.Players.Value = players
		ServerFile.MaxPlayers.Value = maxplayers
		ServerFile.Parent = Servers
		wait(5)
		ServerFile:Destroy()
	end
end)


while game.VIPServerId == "" do
	if game.JobId == nil or game.JobId == "" or game:GetService("RunService"):IsStudio() or game:GetService("RunService"):IsRunMode() then
		return
	end
	local Data = {
		["ServerId"] = game.JobId;
		["Players"] = #game.Players:GetPlayers();
		["MaxPlayers"] = game.Players.MaxPlayers
	}
	
	MessageService:PublishAsync("ServerInfo", Data)
	wait(5)
end

1 Like