· 4 years ago · Mar 09, 2021, 12:20 AM
1--| Services
2local Players = game:GetService("Players")
3local RepStorage = game:GetService("ReplicatedStorage")
4
5--| Remote Functions / Events
6local EventFolder = RepStorage:WaitForChild("Events")
7local FuncFolder = RepStorage:WaitForChild("Functions")
8local CreateLobby = EventFolder:WaitForChild("CreateLobby")
9local DeleteLobby = EventFolder:WaitForChild("DeleteLobby")
10local LeaveLobby = EventFolder:WaitForChild("LeaveLobby")
11local JoinLobby = EventFolder:WaitForChild("JoinLobby")
12local Warning = EventFolder:WaitForChild("Warning")
13
14--| Variables
15local Lobbies = {}
16
17local function CreateTag(Player,TagName,TagValue)
18
19 if not Player.Character:FindFirstChild(TagName) then
20
21 local InLobby = Instance.new("StringValue")
22 InLobby.Name = "In Lobby"
23 InLobby.Value = TagValue
24 InLobby.Parent = Player.Character
25
26 end
27
28end
29
30CreateLobby.OnServerEvent:Connect(function(Player)
31
32 print("Creating Lobby")
33
34 local LobbyName = Player.Name.."'s Lobby"
35
36 --Create a password for the lobby
37 local Number1 = math.random(1,9)
38 local Number2 = math.random(1,9)
39 local Number3 = math.random(1,9)
40 local Number4 = math.random(1,9)
41 local Password = Number1..Number2..Number3..Number4
42
43 print("Lobby Password: "..Password)
44
45 --Add the lobby to the dictionary
46 Lobbies[LobbyName] = {
47 PlayersInLobby = {},
48 Host = Player.Name,
49 Password = Password,
50 }
51
52 local PlayersInLobby = Lobbies[LobbyName].PlayersInLobby
53
54 --Add the player (who is also the host) to the table
55 table.insert(PlayersInLobby,Player.Name)
56
57 --Create a tag for the player
58 CreateTag(Player,"In Lobby",LobbyName)
59
60 CreateLobby:FireAllClients(LobbyName)
61 CreateLobby:FireClient(Player,nil,Password)
62
63end)
64
65DeleteLobby.OnServerEvent:Connect(function(Player)
66
67 print("Deleting Lobby")
68
69 local LobbyToDelete = ""
70
71 if Player.Character:FindFirstChild("In Lobby") then
72 LobbyToDelete = Player.Character["In Lobby"].Value
73 end
74
75 if Lobbies[LobbyToDelete] then
76
77 print("Lobby Exists")
78
79 local PlayersInLobby = Lobbies[LobbyToDelete].PlayersInLobby
80 local Host = Lobbies[LobbyToDelete].Host
81
82 for _,Plr in pairs(PlayersInLobby) do
83
84 if workspace[Plr]:FindFirstChild("In Lobby") then
85 workspace[Plr]:FindFirstChild("In Lobby"):Destroy()
86 end
87
88 if Plr ~= Host then
89 LeaveLobby:FireClient(Players[Plr],nil,nil,true)
90 Warning:FireClient(Players[Plr],"You've been removed from the lobby")
91 end
92
93 end
94
95 Lobbies[Player.Name] = nil
96
97 print("Lobby Deleted")
98
99 DeleteLobby:FireAllClients(LobbyToDelete)
100
101 end
102
103end)
104
105JoinLobby.OnServerEvent:Connect(function(Player,LobbyToJoin)
106
107 print(Player.Name.." is joining "..LobbyToJoin)
108
109 if Lobbies[LobbyToJoin] then
110
111 print('Lobby Exists')
112
113 if #Lobbies[LobbyToJoin].PlayersInLobby ~= 4 then
114
115 local PlayersInLobby = Lobbies[LobbyToJoin].PlayersInLobby
116 local Host = Lobbies[LobbyToJoin].Host
117
118 CreateTag(Player,"In Lobby",LobbyToJoin)
119
120 table.insert(PlayersInLobby,Player.Name)
121
122 for _,Plr in pairs(PlayersInLobby) do
123
124 if Plr ~= Host then
125 JoinLobby:FireClient(Players[Plr],PlayersInLobby)
126 else
127 JoinLobby:FireClient(Players[Host],PlayersInLobby,true)
128 end
129
130 end
131
132 print(PlayersInLobby)
133
134 else
135
136 Warning:FireClient(Player,"Lobby full")
137
138 end
139
140 else
141
142 Warning:FireClient(Player,"Unable to join lobby")
143
144 end
145
146end)
147
148LeaveLobby.OnServerEvent:Connect(function(Player)
149
150 local LobbyToLeave = ""
151
152 if Player.Character:FindFirstChild("In Lobby") then
153 LobbyToLeave = Player.Character["In Lobby"].Value
154 Player.Character["In Lobby"]:Destroy()
155 end
156
157 print(Player.Name.." is leaving "..LobbyToLeave)
158
159 if Lobbies[LobbyToLeave] then
160
161 print("Lobby Exists")
162
163 local PlayersInLobby = Lobbies[LobbyToLeave].PlayersInLobby
164 local Host = Lobbies[LobbyToLeave].Host
165
166 --Remove the player leaving from the table
167 for i,Plr in pairs(PlayersInLobby) do
168 if Plr == Player.Name then
169 table.remove(PlayersInLobby,i)
170 end
171 end
172
173 for _,Plr in pairs(PlayersInLobby) do
174 LeaveLobby:FireClient(Players[Plr],Player.Name,PlayersInLobby)
175 end
176
177 print(PlayersInLobby)
178
179 end
180
181
182end)
183