Teleporting all players to specific location

function teleportPlayers(position)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		player.Character:PivotTo(CFrame.new(position))
	end
end

teleportPlayers(-60, 21.214, -28.5)

This is a code snippet. When it gets to the line that calls the function, this error appears:

ServerScriptService.Story Control:6: invalid argument #1 to ‘new’ (Vector3 expected, got number)

How do I fix the error and why is this happening? it says it’s a number and not a vector3, how do I make it a vector3 instead of a number?

2 Likes

Because your sending it multiple number instead of a vector3.

When calling the function just send it a CFrame value with CFrame.New

Then when using the PivotTo function give it the position parameter

function teleportPlayers(position)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		player.Character:PivotTo(position)
	end
end

teleportPlayers(CFrame.New(-60, 21.2, -28.5))
3 Likes

^ this reply is correct, But you should add a Vector3.new() inside the CFrame.new() since CFrame.new() doesnt accept individual values: turns out it acutally does accept individual values but id still recommend using Vector3.new()

function teleportPlayers(position)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		player.Character:PivotTo(position)
	end
end

teleportPlayers(CFrame.New(Vector3.new(-60, 21.2, -28.5)))

You could also add a failsafe incase the player doesnt currently have a character:

function teleportPlayers(position)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
        if not player.Character then continue end
        if not player.Character:FindFirstChild("HumanoidRootPart") then continue end
		player.Character:PivotTo(position)
	end
end

teleportPlayers(CFrame.New(Vector3.new(-60, 21.2, -28.5)))
4 Likes

Thanks! This works, but I wanted to make it cleaner and easier to write so I did this:

function teleportPlayers(position)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		if not player.Character then continue end
		if not player.Character:FindFirstChild("HumanoidRootPart") then continue end
		player.Character:PivotTo(CFrame.new(position))
	end
end

That also worked

But then i thought, if i could do that, why not this:

function teleportPlayers(position)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		if not player.Character then continue end
		if not player.Character:FindFirstChild("HumanoidRootPart") then continue end
		player.Character:PivotTo(CFrame.new(Vector3.new(position)))
	end
end

This did not work. It teleported me, but it teleported me way into the ground for some reason., even if I change the Y value a lot it still teleports me into the ground. Shouldn’t it be the same? Please explain what’s going on here

In the second example you are creating a new vector 3 with 1 variable. The Vector3.new() constructor wants 3 variables, not just a single one. If you want to do it this way you would need to change the input argument to your function to be X, Y, Z and then instead of position in Vector3.New() youd put the X, Y and Z variables

function teleportPlayers(x, y, z)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		if not player.Character then continue end
		if not player.Character:FindFirstChild("HumanoidRootPart") then continue end
		player.Character:PivotTo(CFrame.new(Vector3.new(x, y, z)))
	end
end

teleportPlayers(427, 73, 682) --Example position
1 Like

CFrame.new() can accept individual values. CFrame.new has multiple different overload functions based on the arguments provided.

1 Like

Thanks! This works but I found an issue where the player’s orientation is reset. How do I keep their current orientation?

In order to keep their orientation im pretty sure you can do this, but i havent tested it yet as im not home currently:

function teleportPlayers(x, y, z)
	local Pos = Vector3.new(x, y, z)
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		if not player.Character then continue end
		if not player.Character:FindFirstChild("HumanoidRootPart") then continue end
		local RootPartCFrame = player.Character.HumanoidRootPart.CFrame
		player.Character:PivotTo(CFrame.LookAt(Pos, Pos + RootPartCFrame.LookVector))
	end
end

teleportPlayers(427, 73, 682) --Example position

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