Matchmaking System with teleports will not work

local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local minPlayers = 2
local lobby = workspace:FindFirstChild("Lobby")

local sps = workspace:FindFirstChild("Spawnpoints")
local spawnPoints = {
	sps.Spawn1,
	sps.Spawn2,
	sps.Spawn3,
	sps.Spawn4,
	sps.Spawn5,
	sps.Spawn6,
	sps.Spawn7,
	sps.Spawn8,
	sps.Spawn9,
	sps.Spawn10,
	sps.Spawn11
	-- Add more spawn points as needed.
}

local gameStarted = false

local function shuffleSpawnPoints()
	for i = #spawnPoints, 2, -1 do
		local j = math.random(i)
		spawnPoints[i], spawnPoints[j] = spawnPoints[j], spawnPoints[i]
	end
end

local function checkPlayerCount()
	local playersInLobby = 0

	for _, player in ipairs(Players:GetPlayers()) do
		if player.Parent == lobby then
			playersInLobby = playersInLobby + 1
		end
	end

	return playersInLobby >= minPlayers
end

local function sendServerMessage(message)
	Chat:Chat(Players:GetPlayers()[1], message, Enum.ChatColor.Blue)
end

local function startCountdown(duration)
	sendServerMessage("SERVER: ROUND STARTING IN " .. tostring(duration) .. " SECONDS")
	for i = duration, 1, -1 do
		wait(1)
	end
end

local function startGame()
	if not gameStarted then
		gameStarted = true
		print("Game is about to start")
		startCountdown(10)
		print("Starting the game...")

		shuffleSpawnPoints()

		local playersInLobby = {}
		for _, player in ipairs(Players:GetPlayers()) do
			if player.Parent == lobby then
				table.insert(playersInLobby, player)
			end
		end

		for i, player in ipairs(playersInLobby) do
			local character = player.Character
			if character then
				local spawnPoint = spawnPoints[i]
				character:SetPrimaryPartCFrame(spawnPoint.CFrame)
			end
		end
	end
end

local function onPlayerAdded(player)
	player.Parent = lobby
	if checkPlayerCount() and not gameStarted then
		startGame()
	else
		print("Waiting for more players. Need at least 2 players to start.")
	end
end

local function onPlayerRemoved(player)
	if player.Parent == lobby then
		if not checkPlayerCount() then
			gameStarted = false
		end
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoved)

My Matchmaking system won’t work, it just keeps repeating that there is not enough players. can somebody help me? I want it to teleport them randomly to one of the 11 spawns I made.

The player’s parent is always going to be game.Players, so no players are being added to the total amount.

1 Like

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