How to Make Players spawn at a random Spawn

Hi, I have a pvp game, but I have an Issue, let me try and explain.

So say that you are player1 and you spawn at green spawn. The next round you spawn at green spawn again, and the next round again etc.

This is my current part of my Script that handles spawns.

        local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		print("Spawnpoints not found")
	end

	local AvailableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs (plrs) do
		if player then
			character = player.Character

			if character then
				-- Teleport them

				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
				table.remove(AvailableSpawnPoints,1)
			end
		end
	end

I tried this, but it did not work.

local AvailableSpawnPoints= SpawnPoints[math.random(1,#AvailableSpawnPoints)]
4 Likes

Try this;

        local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		print("Spawnpoints not found")
	end

	local AvailableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs (plrs) do
		if player then
			character = player.Character

			if character then
				-- Teleport them
                 local Pos = math.random(1,#AvailableSpawnPoints)
                 local SpawnPoint = AvailableSpawnPoints[Pos]
				character:FindFirstChild("HumanoidRootPart").CFrame = SpawnPoint.CFrame + Vector3.new(0,10,0)
				table.remove(AvailableSpawnPoints,Pos)
			end
		end
	end
10 Likes

Works perfectly! Thank you very much!

2 Likes