Hello developers,
I have been working the past couple hours trying to find out how to get a the server list from another place within my game, but I cannot get it to work!
Script so far.
(This script is a failed version of when I tried to get the server list)
ServerscriptService.ServerHandler
local Servers = game:GetService("ReplicatedStorage"):WaitForChild("Servers")
local MS = game:GetService("MessagingService")
local DSS = game:GetService("DataStoreService")
local DS1 = DSS:GetDataStore(120960786437456)
MS:SubscribeAsync("ServerList", function(data)
data = DS1.Data
local serverValue = Instance.new("StringValue", Servers)
serverValue.Name = "Server" .. #Servers:GetChildren() + 1
serverValue.Value = data.serverId.. " " .. data.players
wait(5)
serverValue:Destroy()
end)
StarterGui.GuiHandler
(Responds to ServerHandler creating a new value (the server).)
local function RequestServerCreation()
game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("RequestServer"):FireServer(game:GetService("Players").LocalPlayer)
end
local function updateGui()
local serverFrames = {}
for i, serverValue in pairs(game:GetService("ReplicatedStorage"):WaitForChild("Servers"):GetChildren()) do
local name = serverValue.Name
local serverStats = string.split(serverValue.Value, " ")
local id = serverStats[1]
local plrs = serverStats[2]
local serverFrame = script:WaitForChild("ServerTemplate"):Clone()
serverFrame:WaitForChild("ServerName").Text = name .. "\n ID: " .. id
serverFrame:WaitForChild("PlayerNumbers").Text = plrs .. "/" .. "50"
-- serverFrame:WaitForChild("ServerLocation").Text = serverInfo["country"] .. ", " .. serverInfo["city"]
table.insert(serverFrames, serverFrame)
serverFrame:WaitForChild("JoinButton").MouseButton1Click:Connect(function()
game:GetService("TeleportService"):TeleportToPlaceInstance(120960786437456, id)
end)
script.Parent:WaitForChild("Main"):WaitForChild("ServerList"):ClearAllChildren()
script:WaitForChild("UIListLayout"):Clone().Parent = script.Parent:WaitForChild("Main"):WaitForChild("ServerList")
for i, serverFrame in pairs(serverFrames) do
serverFrame.Parent = script.Parent:WaitForChild("Main"):WaitForChild("ServerList")
end
end
end
updateGui()
script.Parent:WaitForChild("CreateServer").MouseButton1Click:Connect(RequestServerCreation)
game:GetService("ReplicatedStorage"):WaitForChild("Servers").ChildAdded:Connect(updateGui)
game:GetService("ReplicatedStorage"):WaitForChild("Servers").ChildRemoved:Connect(updateGui)
We use the new task library as it the best also we can use pcalls
Server Handler :
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Servers = ReplicatedStorage:WaitForChild("Servers")
local MS = game:GetService("MessagingService")
local DSS = game:GetService("DataStoreService")
local DS1 = DSS:GetDataStore("ServerList_" .. game.PlaceId)
MS:SubscribeAsync("ServerList", function(message)
local data = message.Data
if type(data) == "table" and data.serverId and data.players then
task.spawn(function()
local serverValue = Instance.new("StringValue")
serverValue.Name = "Server" .. (#Servers:GetChildren() + 1)
serverValue.Value = data.serverId .. " " .. data.players
serverValue.Parent = Servers
task.wait(5)
serverValue:Destroy()
end)
end
end)
local function updateServerData()
local success, currentData = pcall(function()
return DS1:GetAsync("CurrentServers") or {}
end)
if success then
MS:PublishAsync("ServerList", {
serverId = game.JobId,
players = #game.Players:GetPlayers()
})
currentData[game.JobId] = {
players = #game.Players:GetPlayers(),
lastUpdate = os.time()
}
DS1:SetAsync("CurrentServers", currentData)
end
end
task.spawn(function()
while true do
updateServerData()
task.wait(30)
end
end)
Gui Handler :
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local Servers = ReplicatedStorage:WaitForChild("Servers")
local Events = ReplicatedStorage:WaitForChild("Events")
local RequestServer = Events:WaitForChild("RequestServer")
local MainGui = script.Parent:WaitForChild("Main")
local ServerList = MainGui:WaitForChild("ServerList")
local CreateServerButton = MainGui:WaitForChild("CreateServer")
local ServerTemplate = script:WaitForChild("ServerTemplate")
local function RequestServerCreation()
RequestServer:FireServer(Players.LocalPlayer)
end
local function updateGui()
local serverFrames = {}
ServerList:ClearAllChildren()
script:WaitForChild("UIListLayout"):Clone().Parent = ServerList
for _, serverValue in ipairs(Servers:GetChildren()) do
local name = serverValue.Name
local serverStats = string.split(serverValue.Value, " ")
local id, plrs = serverStats[1], serverStats[2]
local serverFrame = ServerTemplate:Clone()
serverFrame.ServerName.Text = name .. "\n ID: " .. id
serverFrame.PlayerNumbers.Text = plrs .. "/" .. "50"
serverFrame.JoinButton.MouseButton1Click:Connect(function()
TeleportService:TeleportToPlaceInstance(game.PlaceId, id)
end)
table.insert(serverFrames, serverFrame)
end
for _, frame in ipairs(serverFrames) do
frame.Parent = ServerList
end
end
CreateServerButton.MouseButton1Click:Connect(RequestServerCreation)
Servers.ChildAdded:Connect(function()
task.spawn(updateGui)
end)
Servers.ChildRemoved:Connect(function()
task.spawn(updateGui)
end)
task.spawn(updateGui)
If The issue still presist Please Tell me what error caused it