Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Teleportation script help? (Teleporting all players to a separate location)

So I do not have a script yet but I am going to do a for loop to get all the players to at least spawn at the spawn locations I am going to put in the map.

But here is the question. How would I add to that script in order to get every player to go to their own spawn instead of having a chance of people piling up at a spawn or 2?

-Thanks

1 answer

So what you can do is use a table

local spawns = {}

But it is empty so you have to add all the spawns into it using a for loop

local spawns = {}

for i,v in pairs(game.Workspace.SpawnsFolder:GetChildren()) do
	table.insert(spawns, v)
end

Now when spawning the player at one of the spawns using a random choice, remove the spawn from the table:

local spawns = {}

for i,v in pairs(game.Workspace.SpawnsFolder:GetChildren()) do
	table.insert(spawns, v)
end

--Teleport players
for i,v in pairs(game.Players:GetChildren()) do
	local randomSpawn = spawns[math.random(1, #spawns)] --Sets a variable for a random spawn picked out of the table of 'spawns'
	v.Character.Torso.CFrame = CFrame.new(randomSpawn.Position)
	table.remove(spawns, randomSpawn) --Removes the spawn from the table
end

Hope I helped! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.