How can I teleport players to a map when its cloned?

Ok so I have a script so when a map loads (The map is randomly chosen each round)
how can I teleport those players to the map that is randomly chosen?

2 Likes

You need to have a folder of spawns (invisible, noncancollide parts) that the players can be teleported to inside of the map. Randomly assign the players to be teleported to the spawns when the map is chosen.

1 Like

So how could I teleport them to an assigned spawn inside the map?

Use their HumanoidRootPart, and Position, or CFrame it to positions on the map. Like this:

for a,b in pairs (game.Players:GetPlayers()) do
   local spawn = map.Spawns:GetChildren(map.Spawns:GetChildren()[math.random(0,#map.Spawns:GetChildren())]); -- pick a random spawn
   local char = b.Character or b.Character:Wait();
   char.HumanoidRootPart.CFrame = spawn.CFrame;
end

not sure how much of that is valid lua, you’re going to need to change some of it.

Important parts:

for a,b in pairs (game.Players:GetPlayers()) do
   local char = b.Character or b.Character:Wait();
   char.HumanoidRootPart.Position = Vector3.new(x,y,z);
end
1 Like

Do something like this:

local mapSpawn = map.Spawn

for _, player in pairs(game.Players:GetChildren()) do
	player:MoveTo(mapSpawn.Position)
end
1 Like

This will make the player manually move, so they’ll basically walk there. This method is invalid.

1 Like

You’re thinking of Humanoid:MoveTo(). Doing Player:MoveTo() will automatically bring the player to that position.

1 Like

So the code would be?

if chosenChapter == game.ReplicatedStorage.Chapters.Chapter1Real then

game.Players.LocalPlayer.position = -188.73, 1.5, -186.593

end if chosenChapter == game.ReplicatedStorage.Chapters.Chapter1Real then

game.Players.LocalPlayer.position = -188.73, 1.5, -186.593

end
I know thats completely wrong I just need help with this.

This isn’t a documented API?
30chars

1 Like

Negative. For one don’t move the player on the client like that, do it on the server. Two, Use Position = Vector3.new(x,y,z)

Recommend you reading over this guide
Why, because FIlteringEnabled

1 Like

Whoops I pasted it twice! My bad.

It treats the player as a model, which it technically is. (Model:MoveTo())

image

1 Like

Yeah, I guess that works, however, I think the intended method is using the HumanoidRootPart, though, I guess either way works. I just think its safer as moving certain parts of the body without moving the HRP will kill the player.

1 Like

I personally use the :MoveTo() method because as stated in the Model:MoveTo() api, it avoids collisions. If I were to move the HumanoidRootPart to the position of a flat part, the player could possibly clip inside the ground, but :MoveTo() avoids that, placing them on top every time without needing to add vectors.

Edit: Added api hyperlink because this was marked as solution

1 Like