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

Whats wrong with my teleporting all players script?

Hi I was making a script where after the round everyone gets teleported to a random spawn. But with my script all it does it when the round is over they die can you help?

local spawns = {workspace.Part1.Position,workspace.Part2.Position,workspace.Part3.Position}
for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("Torso") then
      player.Character.Torso.CFrame = CFrame.new(spawns[i])

2 comments

0

Is there an error in the output?

0

no

2 answers

✓ Accepted answer

You describe your script as "teleporting each player to a random spawn". But that's not at all what it says (it doesn't use math.random anywhere!)

Your script says:

For the ith player, teleport them to the ith spawn.

There are only 3 spawns, which means the 4th player wouldn't have anywhere to go.


You should write what you actually said you wanted:

	if player.Character and player.Character:FindFirstChild("Torso") then
		local randomSpawn = spawns[math.random(#spawns)]
		player.Character.Torso.CFrame = CFrame.new( randomSpawn )
	end

Be wary that this will potentially "bunch up" a bunch of players in the same spawn.

laughablehaha's suggestion of MoveTo will help clean that up.

Use MoveTo, instead of CFrame. CFrame will still work, but will bunch up the players, like BlueTaslem said. MoveTo will fix that problem.

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