Currently working on a private server creation system that can be acessed through a GUI. I’m stuck on how I would get the amount of players in the Reserved Server, there are multiple posts like these but they don’t seem to be clear on what to do next. Any help would be appreciated, thanks!
Code:
local serversFolder = game.ReplicatedStorage:WaitForChild("Servers")
local Messenger = require(game.ReplicatedStorage:WaitForChild("Messenger"))
local events = game.ReplicatedStorage:WaitForChild("Events")
local CreateServer = events:WaitForChild("CreateServer")
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PLACE_ID = game.PlaceId
local reservedServers = {}
local rate = 5
local player = game.Players
local ms = Messenger.new("ServerList")
local si = Messenger.new("ServerInfo")
local serverData = {
ServerName = nil,
Description = nil,
VCOn = nil,
ID = nil
}
CreateServer.OnServerEvent:Connect(function(player, Desc, Name, VCMode)
local success, code = pcall(function()
return TeleportService:ReserveServer(PLACE_ID)
end)
if success then
reservedServers[code] = {
Description = Desc,
ServerName = Name,
VCMode = VCMode,
}
si:PublishAsync({
Code = code,
name = Name,
desc = Desc,
vcMode = VCMode
})
TeleportService:TeleportToPrivateServer(PLACE_ID, code, {player}, nil, {Description = Desc, ServerName = Name, VCMode = VCMode})
else
warn("Failed to reserve server; code: " .. tostring(code))
end
end)
si:SubscribeAsync(function(message)
local data = message
serverData.ID = data.Code
serverData.ServerName = data.name
serverData.Description = data.desc
serverData.VCOn = (data.vcMode == "ON")
end)
ms:SubscribeAsync(function(message)
local data = message
game.ReplicatedStorage:WaitForChild("Servers"):ClearAllChildren()
if serverData.ServerName ~= nil and serverData.Description ~= nil and serverData.VCOn ~= nil then
local serverValue = script.ServerName:Clone()
serverValue.Name = "Server" .. #serversFolder:GetChildren() + 1
serverValue.Parent = serversFolder
serverValue.Value = (data.serverId or "[UnknownID]") .. " " .. (data.players or "0") .. " " .. (serverData.ServerName or "[N/A]") .. " " .. (serverData.Description or "[No Description]") .. " " .. tostring(serverData.VCOn)
if data.players == 0 then
serverValue:Destroy()
end
end
end)
while true do
local success, err = pcall(function()
ms:PublishAsync({
serverId = serverData.ID,
serverName = serverData.ServerName,
description = serverData.Description,
players = #game.Players:GetPlayers(), -- this line is the one that needs to get the player in the reserved server, not this one.
vcOn = serverData.VCOn or false
})
end)
if not success then
warn("failed to publish server data: " .. tostring(err))
end
task.wait(rate)
end