How to create a custom server system

What im trying to acheive is to create a custom server system that people can create and also join, only problem, i dont know how to get the data from the reserved servers, which include how many players and the private owner server, im also struggling to make the create server fuction

local

local rep = game.ReplicatedStorage
local serverstorage = rep.SeverFolder
local frames = {}

local function updateserver()
	for i, ServerName in pairs(serverstorage:GetChildren()) do
		local serverStats = string.split(ServerName.Value, "")
		local plr = serverStats[2]
		local ServerId = serverStats[3]
		
		local ServerFrame = script.CloneServerFrame:Clone()
		ServerFrame.ServerName.Text = ServerName.Name
		ServerFrame.PlayerCount.Text = plr.."/20"
		
		table.insert(frames, ServerFrame)
		
		ServerFrame.TextButton.MouseButton1Down:Connect(function()
			game.ReplicatedStorage.FireServerName:FireServer()
		end)
		
		script.Parent.CustomServerFrame.ScrollingFrame:ClearAllChildren()
		
		script.UIGridLayout:Clone().Parent = script.Parent.CustomServerFrame.ScrollingFrame
		
		for i, frames in pairs(ServerFrame) do
			ServerFrame.Parent = script.Parent.CustomServerFrame.ScrollingFrame
		end
	end
end

updateserver()

rep.SeverFolder.ChildAdded:Connect(updateserver)
rep.SeverFolder.ChildRemoved:Connect(updateserver)

script(Unfinished because i gave up)

local DataStorage = game:GetService("DataStoreService")
local tp = game:GetService("TeleportService")
local code = tp:ReserveServer(76645132977147)



game.ReplicatedStorage.FireServerName.OnServerEvent:Connect(function(Plr)
	
end)

and the create server script (Local)

local textbox = script.Parent.Parent.TextBox
local teleportserver = game:GetService("TeleportService")
local servername = ""
local reserved, id = teleportserver:ReserveServer(76645132977147)

textbox.FocusLost:Connect(function(enter)
	textbox.Text = servername
end)

script.Parent.MouseButton1Down:Connect(function()
	game.ReplicatedStorage.FireServerName:FireServer(servername, reserved)
	teleportserver:Teleport(76645132977147, id, game.Players.LocalPlayer)
end)

if you can help me, it would be nice

2 Likes

I have been waiting for 2 hours for a response, please respond

2 Likes

It might be better to try a system with lobbies where all players are sent at once but players can rejoin after they leave, The only way to only allow specific players to join servers is with a reserved server, other methods send players to whatever server isn’t full and are unreliable.

Also, I am assuming that when you say “Local” above the scripts it means that they are LocalScripts which can’t call :ReserveServer() or any of the teleport methods, You need to have a normal script handle server reservation and teleporting players.

1 Like

oh no i changed the script heres the updated version

serverscript service

local DataStorage = game:GetService("DataStoreService")
local ServerCodeStorage = DataStorage:GetDataStore("ServerData")
local tp = game:GetService("TeleportService")
local rep = game.ReplicatedStorage

rep.RemoteEvents.FireCreateServer.OnServerEvent:Connect(function(plr, ServerName)
	local code = tp:ReserveServer(76645132977147)
	local tpOp = Instance.new("TeleportOptions")
	local success, err = pcall(function()
		ServerCodeStorage:SetAsync(ServerName, code, tpOp)
	end)
	if success then
		print("Created Server")
		tp:TeleportAsync(code, {plr}, tpOp)
		local value = Instance.new("StringValue",game.ReplicatedStorage.SeverFolder)
		value.Name = ServerName
		value.Value = code..""..tpOp
	else
		print("Error Creating Server")
	end
end)

rep.RemoteEvents.JoinServer.OnServerEvent:Connect(function(plr, Code)
	local code = string.sub(Code, 1, 6)
	local tpOp = Instance.new("TeleportOptions")
	tp:TeleportAsync(code, {plr}, tpOp)
end)

local script

local rep = game.ReplicatedStorage
local serverFolder = rep.SeverFolder

local function updata()
	for _, Value in pairs(serverFolder:GetChildren()) do
		if Value:IsA("StringValue") then
			local valueSplit = string.split(Value.Value)
			local Id = valueSplit[1]
			local players = valueSplit[2]
			local code = valueSplit[3]
			local name = Value.Name
			local gui = script.ServerName:Clone()
			gui.ServerName.Text = name
			gui.Name = name
			gui.PlayerCount.Text = players.. "/20"
			script.Parent.CustomServerFrame.ScrollingFrame:ClearAllChildren()
			local ui = script.UIListLayout:Clone()
			ui.Parent = workspace
			gui.Parent = script.Parent.CustomServerFrame.ScrollingFrame
		end
	end
end

updata()
serverFolder.ChildAdded:Connect(updata)
serverFolder.ChildRemoved:Connect(updata)
local rep = game.ReplicatedStorage
local text = ""

script.Parent.Parent.TextBox.FocusLost:Connect(function(enter)
	if enter then
		text = script.Parent.Parent.TextBox.Text
	end
end)

script.Parent.MouseButton1Down:Connect(function()
	rep.RemoteEvents.FireCreateServer:FireServer(text)
end)