How to put all players in a row for start a race

  1. What do you want to achieve? At the beginning of a race the players will be teleported in line (each in a specific part) avoiding that two players are teleported in the same part.

image
each player will be teleported on a spot (there are invisible parts)

i did this script using

math.random()

, but two players can be teleported to the same part

btw it is a localscript

local randomnumber = math.random(1,4) --PUT TO 1,4 /!/
	if randomnumber == 1 then
		game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(clonedspain:FindFirstChild("tpblock1").Position)
	elseif randomnumber == 2 then
		game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(clonedspain:FindFirstChild("tpblock2").Position)
	elseif randomnumber == 3 then
		game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(clonedspain:FindFirstChild("tpblock3").Position)
	elseif randomnumber == 4 then
		game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(clonedspain:FindFirstChild("tpblock4").Position)

--etc.etc.etc.
	end
1 Like

For starters, it needs to be a server script that teleports the players.

If you are trying to teleport all players you would need to create a loop.

local AllPlayers = game.Players:GetPlayers()

for i = 1, #AllPlayers do
	for _, player in AllPlayers do
		player.Character:MoveTo(clonedspain:FindFirstChild("tpblock"..i).Position)
	end
end

I haven’t tested the code above so it might not work.

4 Likes

Thank you so much! It works, but i don’t really get it. Can you please explain why you did for _, player in AllPlayers do ?

i represents the number we are currently on in the loop.

Here we are saying for every player in the table AllPlayers we need to move them to their corresponding value in the table.

2 Likes

thanks a lot it’s much clearer now

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.