Why does my code glitch out on the second creation of a lobby?
Asked by
4 years ago Edited 4 years ago
So i am creating a game called Number Quest Ultra which is a game about maths where you create lobbies/games to challenge people. I was testing today when i came across this weird series of problems which makes no sense
Creating a lobby, leaving it and creating another has some freaky behaviour.
The first creation of the lobby is perfectly normal, it creates the lobby, adds me to it and the UI only shows 1 player which is me
Leaving this lobby also works fine, returns to the select lobby screen and removes the lobby.
The second time round seems to have the funny behaviour, this time creating 2 of me on the players list when there should only be 1.
Leaving this does not close the lobby, but instead adds a player to it somehow.
Repeating this just leads to an increase in the player count
Server Side
003 | local mapmodule = require(game.ReplicatedStorage.MapModule) |
004 | local ds = game:GetService( "DataStoreService" ):GetDataStore( "MapChoice" ) |
005 | local ts = game:GetService( "TeleportService" ) |
007 | local lobbytemplate = { |
015 | local function ReturnPlayers(owner,players) |
016 | local newplayers = players |
017 | table.insert(newplayers,owner) |
021 | game.ReplicatedStorage.CreateGame.OnServerEvent:Connect( function (player,gametype,map) |
022 | if mapmodule.Maps [ map ] [ "Type" ] = = gametype then |
023 | if player.Data.Maps:FindFirstChild(map) then |
024 | lobbyid = lobbyid + 1 |
025 | local newlobby = lobbytemplate |
026 | newlobby [ "Owner" ] = player.Name |
027 | newlobby [ "GameType" ] = gametype |
028 | newlobby [ "Map" ] = map |
029 | newlobby [ "LobbyID" ] = lobbyid |
030 | table.insert(lobbies,lobbyid,newlobby) |
031 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
032 | game.ReplicatedStorage.UpdatePlayerList:FireClient(player,ReturnPlayers(newlobby [ "Owner" ] ,newlobby [ "Players" ] )) |
033 | game.ReplicatedStorage.NewLobbyID:FireClient(player,lobbyid) |
038 | game.ReplicatedStorage.LeaveGame.OnServerEvent:Connect( function (player,lobbyid) |
039 | for i,v in pairs (lobbies) do |
040 | if v [ "LobbyID" ] = = lobbyid then |
041 | if v [ "Owner" ] = = player.Name then |
042 | for a,b in pairs (v [ "Players" ] ) do |
043 | game.ReplicatedStorage.LobbyRemoval:FireClient(game.Players [ b ] ) |
045 | table.remove(lobbies,i) |
046 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
048 | for a,b in pairs (v [ "Players" ] ) do |
049 | if b = = player.Name then |
050 | table.remove(v [ "Players" ] ,a) |
051 | for c,d in pairs (v [ "Players" ] ) do |
052 | game.ReplicatedStorage.UpdatePlayerList:FireClient(game.Players [ d ] ,ReturnPlayers(v [ "Owner" ] ,v [ "Players" ] )) |
054 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
062 | game.ReplicatedStorage.JoinGame.OnServerEvent:Connect( function (player,lobbyid) |
063 | for i,v in pairs (lobbies) do |
064 | if v [ "LobbyID" ] = = lobbyid then |
066 | if v [ "GameType" ] = = "Normal" then |
071 | if (#v [ "Players" ] + 1 ) < maxplayers then |
072 | table.insert(v [ "Players" ] ,player.Name) |
073 | for c,d in pairs (v [ "Players" ] ) do |
074 | game.ReplicatedStorage.UpdatePlayerList:FireClient(game.Players [ d ] ,ReturnPlayers(v [ "Owner" ] ,v [ "Players" ] )) |
076 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
082 | game.ReplicatedStorage.KickPlayer.OnServerEvent:Connect( function (player,lobbyid,kickplayer) |
083 | for i,v in pairs (lobbies) do |
084 | if v [ "LobbyID" ] = = lobbyid then |
085 | if v [ "Owner" ] = = player.Name then |
086 | for a,b in pairs (v [ "Players" ] ) do |
087 | if b = = kickplayer then |
088 | table.remove(v [ "Players" ] ,a) |
089 | for c,d in pairs (v [ "Players" ] ) do |
090 | game.ReplicatedStorage.UpdatePlayerList:FireClient(game.Players [ d ] ,ReturnPlayers(v [ "Owner" ] ,v [ "Players" ] )) |
092 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
100 | game.ReplicatedStorage.StartGame.OnServerEvent:Connect( function (player,lobbyid) |
101 | for i,v in pairs (lobbies) do |
102 | if v [ "LobbyID" ] = = lobbyid then |
103 | if v [ "Owner" ] = = player.Name and #v [ "Players" ] > = 1 then |
105 | table.insert(players,game.Players [ v [ "Owner" ] ] ) |
106 | for a,b in pairs (v [ "Players" ] ) do |
107 | table.insert(players,game.Players [ b ] ) |
108 | ds:SetAsync(game.Players [ b ] .UserId,v [ "Map" ] ) |
111 | if v [ "GameType" ] = = "Normal" then |
112 | placeidtp = 5185622956 |
114 | placeidtp = 5185623208 |
116 | table.remove(lobbies,i) |
117 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
118 | local serverid = ts:ReserveServer(placeidtp) |
119 | ts:TeleportToPrivateServer(placeidtp,serverid,players) |
125 | game.Players.PlayerRemoving:Connect( function (player) |
126 | for i,v in pairs (lobbies) do |
127 | if v [ "Owner" ] = = player.Name then |
128 | for a,b in pairs (v [ "Players" ] ) do |
129 | game.ReplicatedStorage.LobbyRemoval:FireClient(game.Players [ b ] ) |
131 | table.remove(lobbies,i) |
132 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
134 | for a,b in pairs (v [ "Players" ] ) do |
135 | if b = = player.Name then |
136 | table.remove(v [ "Players" ] ,a) |
137 | for c,d in pairs (v [ "Players" ] ) do |
138 | game.ReplicatedStorage.UpdatePlayerList:FireClient(game.Players [ d ] ,ReturnPlayers(v [ "Owner" ] ,v [ "Players" ] )) |
140 | game.ReplicatedStorage.UpdateGameList:FireAllClients(lobbies) |
147 | game.ReplicatedStorage.BuyMap.OnServerEvent:Connect( function (player,map) |
148 | if player.Data.Coins.Value > = mapmodule.Maps [ map ] [ "Cost" ] then |
149 | if player.Data.Maps:FindFirstChild(map) = = nil then |
150 | player.Data.Coins.Value = player.Data.Coins.Value - mapmodule.Maps [ map ] [ "Cost" ] |
151 | local newmap = Instance.new( "Folder" ,player.Data.Maps) |
157 | local function RequestLobbies() |
161 | game.ReplicatedStorage.RequestLobbies.OnServerInvoke = RequestLobbies |
Client Side Lobby UI Manager
01 | local function UpdateList(listdata) |
02 | for i,v in pairs (script.Parent:GetChildren()) do |
03 | if v.ClassName = = "Frame" then |
04 | local isavaiable = false |
05 | for a,b in pairs (listdata) do |
06 | if b [ "LobbyID" ] = = v.ID.Value then |
10 | if isavaiable = = false then |
13 | for a,b in pairs (listdata) do |
14 | if b [ "LobbyID" ] = = v.ID.Value then |
15 | if b [ "GameType" ] = = "Normal" then |
16 | v.Players.Text = (#b [ "Players" ] + 1 ).. "/4 Players" |
18 | v.Players.Text = (#b [ "Players" ] + 1 ).. "/16 Players" |
25 | for i,v in pairs (listdata) do |
27 | for a,b in pairs (script.Parent:GetChildren()) do |
28 | if b.ClassName = = "Frame" then |
29 | if b.ID.Value = = v [ "LobbyID" ] then |
34 | if isfound = = false then |
35 | local clone = game.ReplicatedStorage.LobbyFrame:Clone() |
36 | clone.ID.Value = v [ "LobbyID" ] |
37 | clone.Owner.Value = v [ "Owner" ] |
38 | clone.OwnerText.Text = v [ "Owner" ] .. "'s Game" |
39 | clone.Map.Text = v [ "Map" ] |
40 | clone.Mode.Text = v [ "GameType" ] |
41 | if v [ "GameType" ] = = "Normal" then |
42 | clone.Players.Text = (#v [ "Players" ] + 1 ).. "/4 Players" |
44 | clone.Players.Text = (#v [ "Players" ] + 1 ).. "/16 Players" |
46 | clone.Parent = script.Parent |
51 | UpdateList(game.ReplicatedStorage.RequestLobbies:InvokeServer()) |
53 | game.ReplicatedStorage.UpdateGameList.OnClientEvent:Connect( function (lobbies) |
Client Side Player List Manager
01 | local function UpdateList(playerlist) |
02 | for i,v in pairs (script.Parent:GetChildren()) do |
03 | if v.Name = = "PlayerFrame" then |
07 | for i,v in pairs (playerlist) do |
08 | local clone = game.ReplicatedStorage.PlayerFrame:Clone() |
09 | clone.PlayerName.Text = v |
10 | clone.Parent = script.Parent |
14 | game.ReplicatedStorage.UpdatePlayerList.OnClientEvent:Connect( function (playerlist) |
15 | UpdateList(playerlist) |
I am truly confused and just can't understand why this happens