Could someone tell me why others can join my lobbies?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to have a party system to teleport players to their own individual, reserved servers
  2. What is the issue? Include screenshots / videos if possible!
    Other players create a party, but it ends up just joining another player’s party.
  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I haven’t tried many yet, but I have looked into TeleportOptions with an accessCode
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is my Server Script, request to see any more scripts if you need.

local tps = game:GetService("TeleportService")
local placeId = 125864788007071

local function getPlayerParty(playerName)
	for _, party in pairs(game.ReplicatedStorage.Parties:GetChildren()) do
		for _, playerValue in pairs(party.Players:GetChildren()) do
			if playerValue:IsA("StringValue") and playerValue.Value == playerName then
				return party
			end
		end
	end
	return nil
end

local function removePlayerFromParty(playerName)
	local party = getPlayerParty(playerName)
	if party then
		for _, playerValue in pairs(party.Players:GetChildren()) do
			if playerValue:IsA("StringValue") and playerValue.Value == playerName then
				playerValue:Destroy()
				break
			end
		end
	end
end

game.ReplicatedStorage.PartySystemRE.OnServerEvent:Connect(function(player, instruction, value1, value2)
	
	if instruction == "kickPlayer" then
		
		if game.ReplicatedStorage.Parties:FindFirstChild(value1) and value2.Parent == game.ReplicatedStorage.Parties[value1].Players then
			if game.ReplicatedStorage.Parties[value1].Players["Party Leader"].Value == player.Name or value2.Value == player.Name then
				
				if game.ReplicatedStorage.Parties[value1].Players["Party Leader"].Value == value2.Value then
					
					-- Leader is leaving, destroy the whole party
					for i, playerInParty in pairs(game.ReplicatedStorage.Parties[value1].Players:GetChildren()) do
						game.ReplicatedStorage.PartySystemRE:FireClient(game.Players[playerInParty.Value], false, value1)
					end
					
					game.ReplicatedStorage.Parties[value1]:Destroy()
					
				else
					-- Regular player is leaving, just remove them
					game.ReplicatedStorage.PartySystemRE:FireClient(game.Players[value2.Value], false, value1)
					value2:Destroy()
				end
			end
		end
		
	elseif instruction == "createParty" then
		
		-- Check if player is already in a party
		local existingParty = getPlayerParty(player.Name)
		if existingParty then
			game.ReplicatedStorage.PartySystemRE:FireClient(player, false, "You are already in a party!")
			return
		end
		
		local partyName = value1 or player.Name .. "'s Party"		
		partyName = game:GetService("TextService"):FilterStringAsync(partyName, player.UserId)
		partyName = partyName:GetNonChatStringForBroadcastAsync()
		
		-- Ensure unique party name
		local baseName = partyName
		local counter = 1
		while game.ReplicatedStorage.Parties:FindFirstChild(partyName) do
			partyName = baseName .. " (" .. counter .. ")"
			counter = counter + 1
		end
		
		local newParty = Instance.new("Folder")
		newParty.Name = partyName
		
		local players = Instance.new("Folder", newParty)
		players.Name = "Players"
		
		local partyLeader = Instance.new("StringValue", players)
		partyLeader.Name = "Party Leader"
		partyLeader.Value = player.Name
		
		local limit = Instance.new("IntValue", newParty)
		limit.Name = "PlayerLimit"
		limit.Value = tonumber(value2) or 10
		
		local friendsOnly = Instance.new("BoolValue", newParty)
		friendsOnly.Name = "FriendsOnly"
		friendsOnly.Value = value3 or false
		
		newParty.Parent = game.ReplicatedStorage.Parties
		
		game.ReplicatedStorage.PartySystemRE:FireClient(player, true, partyName)
		
	elseif instruction == "joinParty" then
		
		-- Check if player is already in a party
		local existingParty = getPlayerParty(player.Name)
		if existingParty then
			game.ReplicatedStorage.PartySystemRE:FireClient(player, false, "You are already in a party!")
			return
		end
		
		if game.ReplicatedStorage.Parties:FindFirstChild(value1) then
			local party = game.ReplicatedStorage.Parties[value1]
			
			-- Check if party is friends only
			if party:FindFirstChild("FriendsOnly") and party.FriendsOnly.Value then
				local leaderName = party.Players["Party Leader"].Value
				local leader = game.Players:FindFirstChild(leaderName)
				
				if leader and not player:IsFriendsWith(leader.UserId) then
					game.ReplicatedStorage.PartySystemRE:FireClient(player, false, "This party is friends only!")
					return
				end
			end
			
			if #party.Players:GetChildren() < party.PlayerLimit.Value then
				local playerValue = Instance.new("StringValue")
				playerValue.Value = player.Name
				playerValue.Parent = party.Players
				
				game.ReplicatedStorage.PartySystemRE:FireClient(player, true, value1)
			else
				game.ReplicatedStorage.PartySystemRE:FireClient(player, false, "Party is full!")
			end
		else
			game.ReplicatedStorage.PartySystemRE:FireClient(player, false, "Party not found!")
		end
		
	elseif instruction == "startParty" then
		
		if game.ReplicatedStorage.Parties:FindFirstChild(value1) and game.ReplicatedStorage.Parties[value1].Players["Party Leader"].Value == player.Name then
			
			local playersToTP = {}
			
			for i, playerInParty in pairs(game.ReplicatedStorage.Parties[value1].Players:GetChildren()) do
				local plr = game.Players:FindFirstChild(playerInParty.Value)
				if plr then
					table.insert(playersToTP, plr)
				end
			end
			
			-- Reserve a private server for this party
			local accessCode, privateServerId = tps:ReserveServer(placeId)
			
			local tpOptions = Instance.new("TeleportOptions")
			tpOptions:SetTeleportData(accessCode)
			
			tps:TeleportPartyAsync(placeId, playersToTP, tpOptions)
			
			-- Clean up the party after teleporting
			game.ReplicatedStorage.Parties[value1]:Destroy()
		end
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local party = getPlayerParty(player.Name)
	
	if party then
		local isLeader = party.Players["Party Leader"].Value == player.Name
		
		if isLeader then
			-- Leader left, destroy the whole party
			for _, playerInParty in pairs(party.Players:GetChildren()) do
				if playerInParty:IsA("StringValue") then
					local plr = game.Players:FindFirstChild(playerInParty.Value)
					if plr then
						game.ReplicatedStorage.PartySystemRE:FireClient(plr, false, party.Name)
					end
				end
			end
			party:Destroy()
		else
			-- Regular player left, just remove them from the party
			for _, playerValue in pairs(party.Players:GetChildren()) do
				if playerValue:IsA("StringValue") and playerValue.Value == player.Name then
					playerValue:Destroy()
					break
				end
			end
		end
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

TeleportOptions:SetTeleportData is only for sending custom data, not for reserving servers. You need to use TeleportService:TeleportToPrivateServer instead of TeleportPartyAsync

So, replace this:

local accessCode, privateServerId = tps:ReserveServer(placeId)
local tpOptions = Instance.new("TeleportOptions")
tpOptions:SetTeleportData(accessCode)
tps:TeleportPartyAsync(placeId, playersToTP, tpOptions)

with this:

local privateServerId = tps:ReserveServer(placeId)
for _, plr in pairs(playersToTP) do
    tps:TeleportToPrivateServer(placeId, privateServerId, {plr})
end

Alright, I will try this. Thanks!