Im currently making a server list system where you can create a server to add to an existing list of other player created servers for others to join. Ive done this with a UI table system that looks like this.
The way i have thought of going about this, is to create a table whenever a new server is created (we will call this table, PlayerTable). And if another player joins your server from the server list. They get added to your PlayerTable.
Problem is, i dont know how to go about applying that. I dont know how its possible to create a table with a different name whenever a new server is created. Is there actually a way to do it this way? or do i need to change my approach.
Create the PlayerTable on the server (for security reasons) and replicate the server UI for all clients. Whenever a player clicks on the server button it can send a remote function to request that player to enter the server (if it isn’t full), if it is full then the remote function will return false. Here is the server script:
local ServerList = {}
local servercreation = game.ReplicatedStorage.ServerCreation -- remote function, for creating servers
local serverjoin = game.ReplicatedStorage.ServerJoin -- remote function, for joining servers
local serverleave = game.ReplicatedStorage.ServerLeave
servercreation.OnServerInvoke = function(player, data)
if not ServerList[player.UserId] then -- check if a srever already exists for the player
local serverData = {
["Name"] = data["Name"],
["MaxPlayers"] = 8,
["Players"] = {}
}
ServerList[player.UserId] = serverData
return true
end
return false
end
serverjoin.OnServerInvoke = function(player, serverID)
local Server = ServerList[serverID]
if Server and #Server["Players"] < Server["MaxPlayers"] then
for _, plr in pairs(Server["Players"]) do
if plr.Name == player.Name then
return false
end
end
table.insert(ServerList[serverID]["Players"], player)
return true
end
return false
end
serverleave.OnServerInvoke = function(player, serverID)
local Server = ServerList[serverID]
if Server then
for i, plr in ipairs(Server["Players"]) do
if plr.Name == player.Name then
table.remove(Server["Players"], i)
return true
end
end
end
return false
end