Players won't teleport to chosen map's spawn

  1. What do you want to achieve? I want to teleport every player to the chosen map’s spawn.

  2. What is the issue? I’m trying to loop through the players but it won’t work.

  3. What solutions have you tried so far? I can’t think of any other way to do this.

This is a ServerScript:

--// Variables \\--
local RP = game:GetService("ReplicatedStorage")
local Maps = RP:WaitForChild("Maps")
local player = game.Players:GetPlayers()
local intermissionTime = 10

while true do
	wait(intermissionTime)
	local randomMap = math.random(1,1)
	if randomMap == 1 then -- Chosen desert
		print("desert chosen")
		local map = Maps.Desert:Clone()
		map.Parent = game.Workspace
		for i = 1,#player do
			print(player[i].Name)
			local char = player[i].Character
			char.HumanoidRootPart.Position = map.Spawn.Position
		end
	end
end

And no, I’m not getting any errors.

Instead of moving the HumanoidRootPart, trying moving the entire player model:

char:MoveTo(map.Spawn.Position)

Just replace this

		for i = 1,#player do
			print(player[i].Name)
			local char = player[i].Character
			char.HumanoidRootPart.Position = map.Spawn.Position
		end

With this:

for _, Player in pairs(game.Players:GetPlayers()) do
    local char = Player.Character
    if char then
        char.HumanoidRootPart.Position = map.Spawn.Position
    end
end

It’s pretty much the same thing at this point

(I hate case-sensitive words)

Still won’t work :confused:
It won’t even print the players name.

        print(player[i].Name)

Replace this with:

for i, player in pairs(game.Players:GetPlayers()) do
   if player.Character then
      player.Character.HumanoidRootPart.CFrame = map.Spawn.CFrame
   end
end

(Also remove the player variable at the top)
Nevermind, someone already posted the same thing.

3 Likes

Thanks, this worked! :slight_smile:

1 Like

Oh yeah, when teleporting players use CFrame instead of Position.

Is it necessary to do that? The only difference I know is that CFrame also includes the rotation.

I think it’s necessary, but you can test that yourself.
Position: BasePart | Roblox Creator Documentation
CFrame: BasePart | Roblox Creator Documentation