Hello developers, can someone help me? I’m trying to make a system server GUI that will allow players to create and join servers in the main menu, and then start the game.
I got the server adding works in the server list, but now how can I make the join button work? How would I make the join button work so that when the player clicks join, he will join the server that he clicked on?
Here is a video that shows an example:
Now, when you click the join button, it will run this script from a local
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local JoinLobbyEvent = ReplicatedStorage.Remotes.JoinLobby
script.Parent.MouseButton1Down:Connect(function()
JoinLobbyEvent:FireServer(script)
end)
This will make sure that when you click the join button, it will fire to the server for only the button that you clicked, so if there are so many servers, it will join you the only one you clicked.
on the server I have This script handles the server, so when you create a server, it will add it to the list.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerCreateEvent = ReplicatedStorage.Remotes.CreatedServer
local JoinLobbyEvent = ReplicatedStorage.Remotes.JoinLobby
local createdLobbys = {}
ServerCreateEvent.OnServerEvent:Connect(function(Player, SelectChapter, PlayersAmountStats, PrivacyCheck, PasswordCheck)
local ServerTemplate = Player.PlayerGui.Things.ServerTemplate:Clone()
ServerTemplate.Text = Player.Name .. "'s Room | 1/4 Players"
local Lobby = {}
Lobby.Players = {Player}
Lobby.PlayerCount = NumberRange.new(1, PlayersAmountStats)
Lobby.OwnedBy = Player
Lobby.Chapter = SelectChapter
Lobby.PrivacyCheck = PrivacyCheck
Lobby.Password = PasswordCheck
Lobby.ServerTemplate = ServerTemplate
for _, player in ipairs(game.Players:GetPlayers()) do
ServerTemplate:Clone().Parent = player.PlayerGui.MainMenu["Create/Join"].JoinFrame.ServersList
end
table.insert(createdLobbys, Lobby)
local currentPassword = nil
local connection = JoinLobbyEvent.OnServerEvent:Connect(function(player, script, password)
local foundScript = false
for i,v in ServerTemplate:GetDescendants() do
if v == script then
foundScript = true
break
end
end
if foundScript and (not currentPassword or (currentPassword ~= nil and password == currentPassword)) then
local RoomFrame = player.PlayerGui.MainMenu["Create/Join"].RoomFrame
RoomFrame.S1.Text = "Chapter: " .. SelectChapter
RoomFrame.S2.Text = "Players Amount: " .. PlayersAmountStats
RoomFrame.S3.Text = "Privacy: " .. PrivacyCheck
RoomFrame.S4.Text = "Password: " .. PasswordCheck
player.PlayerGui.MainMenu["Create/Join"].JoinFrame.Visible = false
RoomFrame.Visible = true
table.insert(Lobby.Players, player)
elseif not foundScript then
-- bad password or exploiter firing the wrong script
end
end)
ServerTemplate.Destroying:Wait() -- wait until server tab is deleted
connection:Disconnect()
table.remove(createdLobbys, table.find(createdLobbys, Lobby)) -- removes the lobby variable from createdlobbys
end)
and also that it was supposed to join you on the server you clicked, but I don’t know what is wrong.