Problem Description and Request for Solution
Hello everyone
I am developing a server browser system, and I have encountered a frustrating issue. The problem is that while the MessageService:PublishAsync("UpdateServerList", ServerData)
call within a while
loop correctly outputs “success”, it strangely outputs “failed” when using ServerRefresh.OnServerEvent:Connect(function()
.
Despite numerous attempts, I have been unable to resolve this issue. What I want is for “success” to be printed when using ServerRefresh.OnServerEvent:Connect(function()
without the need for the while loop. Specifically, I want ServerRefresh.OnServerEvent:Connect(function()
to print “success” by ensuring that ServerData.ServerId ~= game.JobId
evaluates to true.
Script Code
local MessageService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerRefresh = ReplicatedStorage:WaitForChild("ServerRefresh")
local Servers = ReplicatedStorage:WaitForChild("Servers")
game.ReplicatedStorage.ServerRefresh.OnServerEvent:Connect(function()
if game.VIPServerId == "" then
local ServerData = {
ServerId = game.JobId,
Players = #game.Players:GetPlayers(),
Location = "Tokyo, Japan"
}
MessageService:PublishAsync("UpdateServerList", ServerData)
end
end)
MessageService:SubscribeAsync("UpdateServerList", function(P1)
local ServerData = P1.Data
if ServerData.ServerId ~= game.JobId then
print("success")
local CheckServer = Servers:FindFirstChild(ServerData.ServerId)
if CheckServer then
CheckServer:Destroy()
end
local NewServerData = script:WaitForChild("ServerData"):Clone()
NewServerData.Name = ServerData.ServerId
NewServerData:SetAttribute("ServerId", ServerData.ServerId)
NewServerData:SetAttribute("Players", ServerData.Players)
NewServerData:SetAttribute("Location", ServerData.Location)
NewServerData.Parent = Servers
else
print("failed")
end
end)
MessageService:SubscribeAsync("OtherServerClosed", function(P1)
local ServerData = P1.Data
local CheckServer = Servers:FindFirstChild(ServerData.ServerId)
if CheckServer then
CheckServer:Destroy()
end
end)
game:BindToClose(function()
local ServerData = {
ServerId = game.JobId
}
MessageService:PublishAsync("OtherServerClosed", ServerData)
end)
while true do
if game.VIPServerId == "" then
local ServerData = {
ServerId = game.JobId,
Players = #game.Players:GetPlayers(),
Location = "Tokyo, Japan"
}
MessageService:PublishAsync("UpdateServerList", ServerData)
end
wait(5)
end
Attempts to Resolve
- It appears the issue is within the
ServerRefresh.OnServerEvent:Connect
function. - I want to achieve the same result within the
ServerRefresh.OnServerEvent:Connect
function without using the while loop. - I want
ServerData.ServerId
to be different fromgame.JobId
so that “success” is printed.
Looking forward to your help