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

How to teleport players to separate spawns?

I know how to teleport them all on random spawns, even though it is random, there is a chance of two or more of them spawning on the same spawn. How would I prevent this?

	for i, player in ipairs(game.Players:GetChildren()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			wait(0.01)
			local MapSpawns = ChosenMapClone.MapSpawns:GetChildren()
			local r = math.random(#MapSpawns)
			local MapSpawn = MapSpawns[r]
				player.Character.HumanoidRootPart.CFrame = MapSpawn.CFrame+Vector3.new(0,3,0)

ps: this is only a portion of a script so that's why it's not showing an end

1 answer

✓ Accepted answer

What you want is some way to create a random permutation. Assuming that there are enough spawns to match each player, the following code should work:

local MapSpawns = ChosenMapClone.MapSpawns:GetChildren()

for i, player in ipairs(game.Players:GetChildren()) do
    if player.Character and 
        player.Character:FindFirstChild("HumanoidRootPart") then

        local r = math.random(#MapSpawns)
        local MapSpawn = MapSpawns[r]
        player.Character.HumanoidRootPart.CFrame =
            MapSpawn.CFrame+Vector3.new(0,3,0)
        table.remove(MapSpawns, r)

I also removed wait(0.01) since it seemed unnecessary and could potentially be a source of bugs due to thread scheduling.

[EDIT] If you want to keep the call to wait, move wait(0.01) immediately before the "if" statement.

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