How to make server don't allow players to join it

I have a matchmaking system that teleport 4 players into a new scene, but i have a terrible situation that happens when one of this players teleported to the new scene leaves the scene. The maximum players per server is 4, so if one player leaves the game it will be 3/4. And the server will try to find a new player and teleport him to the server, but the game is started there and i dont want that.
I want to teleport 4 players to one server and dont tp more players there until the game ends and the server tp all players or the players in this scene back.
I’ll appreciate your help because i dont know how to manage this situation.

Thanks!! :smiley:

3 Likes

Are you trying to teleport players to a new place? Or to a new location in the same place?

3 Likes

You should be able to use

TeleportService:TeleportToPrivateServer()
and
TeleportService:ReserveServer()

1 Like

I teleport them to a new scene using the teleport service :slight_smile:

1 Like

So this will only let the players i sent to this server join them until is full and after that no more players can join this one, right?

2 Likes

Yep! It teleports the players and can only be joined back if you manually teleport them and provide the Access Code given by ReserveServer()

3 Likes

this is my code but I don’t know how can I implement the ReserveServer() and the TeleportOptions.ReservedServerAccessCode parameter.
Here is my code if you can help me to fix it.

local function TeleportPlayers()
if isTeleporting then
	return
end
isTeleporting = true
for i = 1, LobbySize do  
	local player = LobbyPlayers[i]
	if player then
		local kartSelected = player.Kart_Selected.Kart.Value
		local recolorSelected = player.Kart_Props.Recolor.Value
		local flagSelected = player.Kart_Props.Flag.Value
		
		local tpData = { Kart = kartSelected, Recolor = recolorSelected, Flag = flagSelected, }
		local teleportOptions = Instance.new("TeleportOptions")
		teleportOptions:SetTeleportData(tpData)			

		print("Player => ", player.Name)
		print("KartSelected => ", kartSelected)
		print("Table Data => ", tpData)
		
		wait(1)
		player:WaitForChild("PlayerGui"):SetAttribute("racePlayed", true)
		player:WaitForChild("PlayerGui").PopUps.Play.Frame.Play.Text = "ON QUEUE"
		teleportService:TeleportAsync(placeID_Race_1, {player}, teleportOptions.ReservedServerAccessCode)  
	end
end

for i = LobbySize, 1, -1 do 
	table.remove(LobbyPlayers, i)
end

isTeleporting = false

end

I can assist you more when I’m at home, give me an hour or so & I can try and help you further

1 Like

sure, take your time mate, I’ll be here so dont worry :slight_smile:

Try this:

local teleportService = game:GetService("TeleportService")
local function TeleportPlayers()
	if isTeleporting then
		return
	end
	isTeleporting = true
	for i = 1, LobbySize do  
		local player = LobbyPlayers[i]
		if player then
			local kartSelected = player.Kart_Selected.Kart.Value
			local recolorSelected = player.Kart_Props.Recolor.Value
			local flagSelected = player.Kart_Props.Flag.Value

			local tpData = { Kart = kartSelected, Recolor = recolorSelected, Flag = flagSelected, }
			local AccessCode = teleportService:ReserveServer(placeID_Race_1)	

			print("Player => ", player.Name)
			print("KartSelected => ", kartSelected)
			print("Table Data => ", tpData)

			wait(1)
			player:WaitForChild("PlayerGui"):SetAttribute("racePlayed", true)
			player:WaitForChild("PlayerGui").PopUps.Play.Frame.Play.Text = "ON QUEUE"
			teleportService:TeleportToPrivateServer(placeID_Race_1, AccessCode, {player}, tpData)  
		end
	end

	for i = LobbySize, 1, -1 do 
		table.remove(LobbyPlayers, i)
	end

	isTeleporting = false

Swapped out your teleport code w/

local AccessCode = teleportService:ReserveServer(placeID_Race_1)
and
teleportService:TeleportToPrivateServer(placeID_Race_1, AccessCode, {player}, tpData)

Let me know if it works as intended!

2 Likes

Wouldn’t this be something like this …

local maxPlayers = 4 -- Maximum players allowed in the game
local lobbyPlaceId = 123456789 -- Replace with the PlaceID of your lobby game

game:GetService("Players").PlayerAdded:Connect(function(player)
    local playerCount = #game:GetService("Players"):GetPlayers()
    
    if playerCount > maxPlayers then
        game:GetService("TeleportService"):Teleport(lobbyPlaceId, player)
    end
end)

With maybe a message it was full before you send them back …
Not sure you can check number of players in a game atm from a different game.
So I would think this would be straight forward …

The lobby (a different game) could keep track of that I guess. But still sounds complicated.
Need private servers for that to work out.

1 Like

I think it’s not working because we need to put the servercode on the other scene, right?