Lobby not detecting new player inside of folder?

Hey! A few days ago I had dissected another persons lobby GUI system and am taking a crack at making my own! It has been going pretty smoothly so far but there is a minor hiccup that I have ran into.

Whenever a player joins the lobby, their name gets added as a stringValue to a folder and when the player requests to join by clicking the join button. That specified lobby is searched through and if the players name isn’t found, they are added and if it is found. nothing should happen.

That is in theory how it SHOULD be working but the problem I’m running into is that the button can be clicked multiple times which keeps creating new stringValue instances of the player. This obviously is not what is supposed to be happening. Below I have linked a video demonstrating the problem with the Lobby folder open along with the output.


I do not know what I’m missing because I have looked over my own script a few times and can’t wrap my head around what is happening.

Function that updates all joinable lobbies: (Local Script)

local function lobbyPageUpdate() --Updates the lobby page with all available lobbies.
	
	print("Updating lobby page!")
	guiRefresh(masterFrame.LobbyPage.ScrollingLobby)
	
	for _, lobby in pairs(lobbies:GetChildren()) do
		if lobby:IsA("Folder") then
			
			local lobbyClone = lobbyPreset:Clone()
			local leader = findPlayerType(lobby, "Leader")
			
			lobbyClone.LobbyImage.Image = getAvatar(leader.Name)
			lobbyClone.LobbyName.Text = leader.Name.."'s Lobby"
				
			lobbyClone.Parent = masterFrame.LobbyPage.ScrollingLobby
			
			lobbyClone.Join.MouseButton1Click:Connect(function()
				
				--print("Sending request to join "..lobby.Name)
				lobbyRemote:FireServer("Join", lobby)
				
			end)	
		end
	end
end

instructions sent to the server where the player and lobby data is caught: (Server Script)

lobbyRemote.OnServerEvent:Connect(function(plr, instruction, data)
	
	if instruction == "Create" then
		
		for _, currentLobbies in pairs(lobbies:GetChildren()) do
			if currentLobbies.Name == plr.UserId.."'s Lobby" then
				print("Player already created a lobby!")
				return
			end
		end
		
		--print("Creating new lobby")
		
		local newLobby = Instance.new("Folder")
		newLobby.Name = plr.UserId.."'s Lobby"
		newLobby.Parent = lobbies
		
		local newMember = Instance.new("StringValue")
		newMember.Name = plr.Name
		newMember.Parent = newLobby
		
		local leader = Instance.new("StringValue")
		leader.Name = "Leader"
		leader.Parent = newMember
		
	--This is the problem area I believe	
	elseif instruction == "Join" then
		
		print(plr.Name.." requested to join " ..data.Name) --The player that requested to join and the lobby they are joining.
		
		for _, member in pairs(data:GetChildren()) do
			if member.Name == plr.Name then
				print("Player is already in this lobby!")
				return
			else
				print("Player is not in this lobby!")
				local newMember = Instance.new("StringValue")
				newMember.Name = plr.Name
				newMember.Parent = data
				
			end
		end
	end
end)

Haven’t read everything yet, but could you check if the Player1 appears inside Lobby folder on server

This is in the server view, it appears that they both do show up. Something else to note is that it does not duplicate the person who makes the lobby.

When you check the first object in the Lobby Folder and if it is Player2 - it adds the new player. You should first check whole Folder for the player and only then add him if he wasn’t found. That’s why it prints both that he is lobby(+adding him) and he is not in lobby after seeing second object that is Player1 ending the loop.

How you actually should do:

local found = false

for _, member in pairs(data:GetChildren()) do
	if member.Name == plr.Name then
		print("Player is already in this lobby!")
		found = true
		break -- stop early if found
	end
end

if not found then
	print("Player is not in this lobby!")
	local newMember = Instance.new("StringValue")
	newMember.Name = plr.Name
	newMember.Parent = data
end
1 Like

Okay, this certainly makes sense! But just to reiterate on why mine wasn’t working in the first place, the entire folder was being checked, but since it was looping through every item inside the folder, it would find that newly added player and then create another one?

How it worked:

We have folder with two elements:
Player 2
Player 1

For loop checks Player2, sees that it is not Player1 and adds another Player1, continues to go thru the objects, finds Player1 and ends the loop. But now there are two Player1 objects.

Basically it starts from the first object that was added, which was Player2 and your script works in such way that it adds new Player even after checking just one object.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.