Find undefined table using one of the tables properties

Hi! I know the title sounds confusing. But let me explain.

I’m working on a lobby system for my game. For other clients to join, I need to find the “lobby” object within the lobbies table, however it’s undefined and I dont know how to find the lobby when I don’t have it defined

example of what the “lobby” would look like

full module script

local replicatedStorage = game:GetService("ReplicatedStorage")

local lobbySystem = {}

lobbySystem.__index = lobbySystem

lobbySystem.lobbies = {}

function lobbySystem.New(player)
	local self = setmetatable({}, lobbySystem)
	self.host = player
	self.players = {}
	self.maxPlayers = 4
	self.currentPlayerCount = 0
	
	table.insert(lobbySystem.lobbies, self)
	
	return self
end


function lobbySystem:AddPlayer(player)
	table.insert(self.players, player)
	print(self)
end

function lobbySystem:CheckPlayerCount()
	local count = 0
	
	for _, player in self.players do
		if not player:IsA("Player") then continue end
		
		count += 1
	end
	
	return count
end

function lobbySystem:Create() -- Create a lobby instance and send it to clients
	replicatedStorage.events.createParty:FireAllClients(self.host)
end

function lobbySystem:InvitePlayer(player: Player)
	if self.currentPlayerCount < self.maxPlayers then
		self.players[player.UserId] = player
		self.currentPlayerCount += 1
		print("Player invited to lobby:", player.Name)
	else
		print("Lobby is full. Cannot invite player:", player.Name)
	end
end

return lobbySystem

server script

local replicatedStorage = game:GetService("ReplicatedStorage")

local lobbySystem = require(replicatedStorage.modules.logic.lobbySystem)

replicatedStorage.events.createParty.OnServerEvent:Connect(function(player)
	local newLobby = lobbySystem.New(player)
	newLobby:Create()
	newLobby:AddPlayer(player)
end)

replicatedStorage.events.joinParty.OnServerEvent:Connect(function(player, hostName)
	local host = game.Players:FindFirstChild(hostName)
	
	local lobby = "??"
end)

client script

local replicatedStorage = game:GetService("ReplicatedStorage")

local events = replicatedStorage:WaitForChild("events")

local createParty = events:WaitForChild("createParty")

local mainFrame = script.Parent:WaitForChild("mainFrame")
local scrollingFrame = mainFrame:WaitForChild("ScrollingFrame")

local createButton = mainFrame:WaitForChild("createButton")

local isInParty = mainFrame:WaitForChild("isInParty")

createButton.MouseButton1Click:Connect(function()
	if isInParty.Value == false then
		createParty:FireServer()
		isInParty.Value = true
	end
end)

createParty.OnClientEvent:Connect(function(host)
	local uiTemplate = replicatedStorage.uiTemplates:WaitForChild("partyUI"):Clone()
	uiTemplate.Parent = scrollingFrame
	
	uiTemplate.hostName.Text = host.Name
end)

hopefully that isn’t too confusing! anyways again I want to have a reference of the lobby the player is joining , and to find the lobby inside the “lobbySystem.lobbies” table.

Try updating your JoinParty.OnServerEvent like this

	local foundLobby = nil

	for _, lobby in ipairs(lobbySystem.lobbies) do
		if lobby.host and lobby.host.Name == hostName then
			foundLobby = lobby
			break
		end
	end

	if foundLobby then
		foundLobby:InvitePlayer(player)
	else
		warn("No lobby found for host:", hostName)
	end
end)


--[[When a player creates a lobby, an object (table) is created with self.host = player.

You store that object in lobbySystem.lobbies.

When someone wants to join a specific lobby by host name, you just loop through the lobbies and match the host.Name.

This might work lol