How to teleport one player to another?

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local uis = game:GetService("UserInputService")


Mouse.Move:Connect(function()
	local target = Mouse.Target
	if (not target) then return end

	local plr = game:GetService('Players'):GetPlayerFromCharacter(target.Parent)
	if plr then
		uis.InputBegan:Connect(function(Key)
			if Key.KeyCode == Enum.KeyCode.Q then
				Player.Character.HumanoidRootPart.Position = plr.Character.HumanoidRootPart.Position
				
				return
			end
		end)
	end
end)

So I have a script that if you point the cursor at another player and press Q. Then I would like the first player (me) teleport, in front of the second player. That’s just when I do what you see in the script is something strange, the camera recedes and everything seems to break

3 Likes

TL;DR: If you just want the script, scroll down to the last script…

The rest just explains why it doesn’t work.


Don’t move the player with the Position property, it won’t work. The reason why, is because it’d destroy the limbs. That goes with any type of welding, you can test this yourself when you move a part welded to another part in studio.

Thanks for the info. But then, how do you move the player?

You instead should move the player with CFrames. It’s almost the same thing as using Position, except you’d can set the rotation. (since CFrame is basically the Position and Orientation property for a BasePart)

In your example, we can just change the script up to using the CFrames.

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local uis = game:GetService("UserInputService")


Mouse.Move:Connect(function()
	local target = Mouse.Target
	if (not target) then return end

	local plr = game:GetService('Players'):GetPlayerFromCharacter(target.Parent)
	if plr then
		uis.InputBegan:Connect(function(Key)
			if Key.KeyCode == Enum.KeyCode.Q then
				Player.Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame			
				return
			end
		end)
	end
end)

And now it should be able to teleport the player!


Okay, nice. But I wanted to teleport them in front of the player. So…

How do I do just that?

You can just an offset to the final CFrame’s position. You can either use a Vector3 for the offset, (then you’d need to add the Vector3 to the CFrame. CFrame + Vector3) or you use another CFrame. (to which, you’d then need to multiply the two CFrames together. CFrame * CFrame)

I’d also assume you’d want to have the player who’s teleporting to the other player, look at the other player when they teleport. You’d probably think this is a hard thing to do, but in fact, it’s not!.

You can use several things to make a CFrame (like 6 different things) when you do CFrame.new(). But in those options, there’s an option where you input two Vector3’s. The first parameter will represent its position, and the second is the lookAt from the specified position. Its name should be obvious as to why it’s called that.

Back to the original question, though. To do just that, you can just use the player’s current HumanoidRootPart position as the CFrames lookVector. And, since we are going to be offsetting the position of the player being teleported, we should have variables to represent the offset, and the position of the HumanoidRootPart. All of these changes, would make a script like this:

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local uis = game:GetService("UserInputService")


Mouse.Move:Connect(function()
	local target = Mouse.Target
	if (not target) then return end

	local plr = game:GetService('Players'):GetPlayerFromCharacter(target.Parent)
	if plr then
		uis.InputBegan:Connect(function(Key)
			if Key.KeyCode == Enum.KeyCode.Q then
				local position = plr.Character.HumanoidRootPart.CFrame.Position
				local offset = Vector3.new(3, 0, 3)
				
				Player.Character.HumanoidRootPart.CFrame = CFrame.new(position + offset, position)
				return
			end
		end)
	end
end)

Hopefully this all works, and if it doesn’t, just make another post or reply to this post regarding it. Otherwise, happy coding and good luck making your game!

4 Likes

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