So, This brings all players in the server to a spawner, but the problem is they all go to the same spawner. I need 1 player per spawner. (6 Spawners total)
01 | function BringPlayers() |
02 | Spawns = game.Workspace.MapSpawners:GetChildren() |
03 | local SelectedSpawn = math.random( 1 ,#Spawns) |
04 | local Spawn = Spawns [ SelectedSpawn ] |
05 |
06 | local Players = game.Players:GetPlayers() |
07 | for i = 1 , #Players do |
08 | Players [ i ] .Character.UpperTorso.CFrame = CFrame.new(Spawn.SpawnPart.Position) |
09 | end |
10 | end |
You need to define the Spawn
and SelectedSpawn
varaibles inside of the for loop, so that the math.random
result varies :)
EDIT: Remove values from the 'Spawns' table, using the table.remove
function to ensure nobody gets duplicate spawns :)
01 | function BringPlayers() |
02 | local Spawns = workspace.MapSpawners:GetChildren() |
03 | local Players = game.Players:GetPlayers() |
04 | for i = 1 , #Players do |
05 | --Define inside for loop |
06 | --EDIT: table.remove to ensure no duplicates |
07 | local SelectedSpawn = table.remove(Spawns,math.random(#Spawns)) |
08 | local Spawn = Spawns [ SelectedSpawn ] |
09 | if Players [ i ] .Character then --Make sure they are spawned in |
10 | --Rootpart for R6/R15 compatibility |
11 | local root = Players [ i ] .Character.HumanoidRootPart |
12 | root.CFrame = Spawn.SpawnPart.CFrame |
13 | end |
14 | end |
15 | end |
The problem is that you select only one random spawn location for everyone. Try this:
01 | function BringPlayers() |
02 | local Spawns = game.Workspace.MapSpawners:GetChildren() |
03 | local UsedSpawns = { } |
04 | local Players = game.Players:GetPlayers() |
05 | for i = 1 , #Players do |
06 | local SelectedSpawn = math.random( 1 ,#Spawns) |
07 | local AlreadyUsed = true |
08 | while AlreadyUsed do |
09 | AlreadyUsed = false |
10 | for i,v in pairs (UsedSpawns) do |
11 | if v = = SelectedSpawn then |
12 | AlreadyUsed = true |
13 | end |
14 | end |
15 | if AlreadyUsed then |
This while loop keeps trying to find an unused spawn location. There might be a more efficient way of doing this but this is what I thought of.