for i,player in pairs(game.Players:GetPlayers())do local num = math.random(6) local torso=player.Character:FindFirstChild("Torso") if torso then if player.TeamColor == BrickColor.new("Really blue") then if num == 1 then torso.CFrame=CFrame.new(game.Workspace.City.BlueSpawns.Spawn1.Position + Vector3.new(0,i*5,0)) elseif num == 2 then torso.CFrame=CFrame.new(game.Workspace.City.BlueSpawns.Spawn2.Position + Vector3.new(0,i*5,0)) elseif num == 3 then torso.CFrame=CFrame.new(game.Workspace.City.BlueSpawns.Spawn3.Position + Vector3.new(0,i*5,0)) elseif num == 4 then torso.CFrame=CFrame.new(game.Workspace.City.BlueSpawns.Spawn4.Position + Vector3.new(0,i*5,0)) elseif num == 5 then torso.CFrame=CFrame.new(game.Workspace.City.BlueSpawns.Spawn5.Position + Vector3.new(0,i*5,0)) elseif num == 6 then torso.CFrame=CFrame.new(game.Workspace.City.BlueSpawns.Spawn6.Position + Vector3.new(0,i*5,0)) end end end end
This script basically has each player get a random spawn to go to. However, I'd like to know if there's a good way to make it so that two players can't get assigned the same spawn. I'd appreciate some help with this.
First, cleanup.
workspace
instead of game.Workspace
to save yourself a lot of typing and readingWhen you find yourself doing something like Spawn1
, Spawn2
, Spawn3
, it means it's time to use a list.
Supposing that BlueSpawns
is just those 6 spawn locations, you can easily get a list of them using :GetChildren()
:
-- List of all spawns: local potentialSpawns = workspace.City.BlueSpawns:GetChildren() for i, player in pairs(game.Players:GetPlayers()) do local torso = player.Character:FindFirstChild("Torso") if torso then if player.TeamColor == BrickColor.new("Really blue") then -- Pick a random spawn from the list: local index = math.random(#potentialSpawns) local randomSpawn = potentialSpawns[index] -- Move to that random choice: torso.CFrame = CFrame.new(randomSpawn.Position + Vector3.new(0, i*5, 0)) end end end
This code does the same thing that your code does, but it's better formatted and half the length. This makes it much easier to start with!
The behavior that you want is
"two players can't get assigned to the same spawn".
We can state this in a slightly more specific way:
Once a spawn has been chosen, that spawn should not be chosen again
We need to somehow "disable" that spawn from being selected again. Since right now we're just choosing a random thing from potentialSpawns
, the easiest way is to remove that from potentialSpawns
once we pick it.
In fact, that requires only one very small change to do:
local potentialSpawns = workspace.City.BlueSpawns:GetChildren() for _, player in pairs(game.Players:GetPlayers()) do local torso = player.Character:FindFirstChild("Torso") if torso then if player.TeamColor == BrickColor.new("Really blue") then local index = math.random(#potentialSpawns) -- give table.remove an index, and it gives you what WAS there -- and simultaneously removes it: local randomSpawn = table.remove(potentialSpawns, index) -- since you can't repeat spawns, no need for that i*5 torso.CFrame = CFrame.new(randomSpawn.Position + Vector3.new(0, 5, 0)) end end end
You'll of course run into issues if there are more players on this team than spawns -- be extremely careful that that does not happen -- or deal with it directly.