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.
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
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)
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.