Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How do I make this spawn 1 player per spawner? Help?

Asked by 7 years ago

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)

01function 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

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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 :)

01function 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
15end
0
Thanks! IfIWasntSoSwag 98 — 7y
1
But there is still a possibly that two players spawn at the same location. Bluemonkey132 194 — 7y
0
You're right Bluemonkey ;D table.remove applied. Goulstem 8144 — 7y
Ad
Log in to vote
1
Answered by 7 years ago

The problem is that you select only one random spawn location for everyone. Try this:

01function 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
View all 23 lines...

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.

Answer this question