How to make a player not spawn on a spawn that someone spawned in already?

local playersInRound = {} -- track alive players
local connections = {} -- store connections to disconnect later
for _, Player in pairs(Players:GetChildren()) do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
		local RandomSpawn = Spawns[math.random(1, #Spawns)]
		Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
		local newSword = Sword:Clone()
		newSword.Parent = Player.Backpack

		table.insert(playersInRound, Player) -- add all players to table
		connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
			table.remove(playersInRound, table.find(playersInRound, Player))
			
			
		end)
	end
end
2 Likes

This belongs in #help-and-feedback:scripting-support not in #development-discussion

2 Likes

bro i dont even know who to listen to, the guy above u said the opposite

1 Like

Add a BoolValue in each spawn and default it to false. Then if you want to spawn a player, check each of the spawn if the BoolValue is false. If it is false, then we spawn the player on that spawn and set the BoolValue of that spawn to true, If it is true, we find another spawn. If every spawn’s BoolValue is true, then warn the player that every spawn is occupied

1 Like

It would be in scripting support as you are asking for support for your script.

But to answer your question, store the spawns in a table, then remove the spawn you picked for a player from the table every time

local playersInRound = {} -- track alive players
local connections = {} -- store connections to disconnect later

for _, Player in pairs(Players:GetChildren()) do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
        local randSpawn = math.random(1, #Spawns)
		local RandomSpawn = Spawns[randSpawn]
        table.remove(Spawns, randSpawn)
		Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
		local newSword = Sword:Clone()
		newSword.Parent = Player.Backpack

		table.insert(playersInRound, Player) -- add all players to table
		connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
			table.remove(playersInRound, table.find(playersInRound, Player))
			
			
		end)
	end
end
1 Like

You can do it like this:

local playersInRound = {} -- track alive players
local connections = {} -- store connections to disconnect later
for _, Player in pairs(Players:GetChildren()) do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
		local RandomSpawn = Spawns[math.random(1, #Spawns)]
        if RandomSpawn:WaitForChild("BoolValue").Value == false then
           RandomSpawn.BoolValue.Value = true
		   Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
		   local newSword = Sword:Clone()
		   newSword.Parent = Player.Backpack

		   table.insert(playersInRound, Player) -- add all players to table
		   connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
			    table.remove(playersInRound, table.find(playersInRound, Player))
			
			
		    end)
        end
	end
end
1 Like

This could work, but it has a problem. If a player has already spawned on location 1, but it randomly picks location 1 again, the next player just wouldn’t spawn. You could fix this by looping until there is an available location, but if they all ended up false for some reason you would be trapped in an infinite loop or skip them again.

Oops, totally forgot about that. Sorry. But here’s the fix:

local playersInRound = {} -- track alive players
local connections = {} -- store connections to disconnect later


local function findSpawn()
	local SpawnsTable = {}
	for _,plrSpawn in pairs(Spawns:GetChildren()) do
		if plrSpawn:WaitForChild("BoolValue").Value == false then
			table.insert(SpawnsTable, plrSpawn)
		end
	end
	local RandomSpawn = Spawns:FindFirstChild(SpawnsTable[math.random(1, #SpawnsTable)].Name)
	if RandomSpawn ~= nil then
		print("Hello")
		return RandomSpawn
	end	
end


for _,Player in pairs(game.Players:GetPlayers()) do
	local character = Player.Character or Player.CharacterAdded:Wait()
	if character and character:FindFirstChild('Humanoid') then
		local RandomSpawn = findSpawn()
		if RandomSpawn ~= nil then
			if RandomSpawn:WaitForChild("BoolValue").Value == false then
				RandomSpawn.BoolValue.Value = true
				character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				local newSword = Sword:Clone()
				newSword.Parent = Player.Backpack

				table.insert(playersInRound, Player) -- add all players to table
				connections[Player.Name] = character.Humanoid.Died:Connect(function()
					table.remove(playersInRound, table.find(playersInRound, Player))
				end)
			end
		end
	end
end