I am trying to to make it where it teleports all the players to different spawnpoints and not pick the same spawnpoint twice

I am trying to to make it where it teleports all the players to different bricks and not repeat the same brick over and over.

So basically i have a map it has 12 spawnpoints. The script currently chooses how to pick the spawnpoint for every player with a math.random but sometimes it picks the same spawnpoint twice so 2 people spawn on it and it just ruins the experince. I want to fix this but cant come up with a solution. I just want it where when a player spawns on the spawpoint no one else can spawn on it next.

heres the script:


	for _,player in pairs(game:GetService("Players"):GetPlayers())do
		if player and player.Character then
			
			local selectedLocation = map.SpawnLocations:GetChildren()[math.random(1,#map.SpawnLocations:GetChildren())]  -- this is the part where it chooses the spawpoint
			player.Character:MoveTo(selectedLocation.Position)

You need to make a new table and remove all the ones you use. Warning that this will not work once it ran out of spawnpoints:

local spawnPoints = map.SpawnLocactions:GetChildren()

for _,player in pairs(game:GetService("Players"):GetPlayers()) do
    if player and player.Character then
        local selected = math.random(1,#spawnPoints)
        local selectedLocation = spawnPoints[selected] table.remove(spawnPoints,selected)
        player.Character:MoveTo(selectedLocation.Position)
    ...end
...end
2 Likes

thank you it worked and is there anyway if theres not enoguh spawnpoint it just defaults back or? :heart:

Simple. For each spawn point, you can create a value inside of that spawn point.

The code should look something like this::

local spawnpoints = ... -- this variable should contain the folder/model with your spawnpoints.

function getSpawn()
   local x = math.random(0, #spawnpoints:GetChildren())
   if not spawnpoints[x].hasPlayer.Value then
    spawnpoints.hasPlayer.Value = true
    return spawnpoints[x]
   else
     getSpawn()
   end
end

for _, plr in pairs(game.Players:GetPlayers()) do
  local spawn = getSpawn()
  -- your code here
end

The number of spawn points should correspond with the server’s player capacity, that way each potential player will get a unique spawn location.

1 Like

could you eloborate please. i dont get this