Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I teleport a Player by adding/subtracting their x and z position?

Asked by 6 years ago
Edited 6 years ago

I want to be able to teleport a player by adding or subtracting a number to their x and z position. ex. How can I add 50 to a player's x position and subtract 30 from their z with a command?

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This should work.

-- define the variable "character" up here
local humroot = character:FindFirstChild("HumanoidRootPart") -- We need to move specifically the HumanoidRootPart - the primary part of the character; elsewise we will separate the character's limbs and the player will die.
local x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22 = humroot.CFrame:Components() -- get the components of the humroot's CFrame. All the "r__" thingies are the humroot's CFrame rotation matrix; we want that data so we don't accidentally reset the rotation of the character.

humroot.CFrame = CFrame.new(x + 50, y, z - 30, r00, r01, r02, r10, r11, r12, r20, r21, r22) -- Set the humroot's X CFrame to its current + 50, set its Y to its current, and subtract 30 from the Z. Then we add in the current rotation matrix.

(Note: You cannot use humroot.Position because, if I'm not mistaken, that'll just kill the character.)

Ad
Log in to vote
0
Answered by 6 years ago

To teleport a player, you set the HumanoidRootPart's CFrame to the CFrame you want to teleport it to. Like this:

workspace.Player.HumanoidRootPart.CFrame = CFrame.new(0,0,0)

And because you can access a CFrame's X, Y, or Z and CFrames are made of 3 arguments, you could try to figure out how you would make the CFrame in this case. Something like this:

CFrame.new(X+50,Y,Z-50)

Now that you have figured out the solution, you can now teleport the HumanoidRootPart to the CFrame, but you'll have to define X, Y, and Z.

local X = workspace.Player.HumanoidRootPart.CFrame.X
local Y = workspace.Player.HumanoidRootPart.CFrame.Y
local Z = workspace.Player.HumanoidRootPart.CFrame.Z
workspace.Player.HumanoidRootPart.CFrame = CFrame.new(X+50,Y,Z-50)

Hope this helped!

Log in to vote
0
Answered by 6 years ago

This is actually quite simple, and you only need to know the basics of CFraming and Vector#. All you have to do is get the players head or torso or somethings CFrame, and change it to the same CFrame minus or added a Vector3 Value.

-- Vector3 and CFrame work like .new(x, y, z)
script.Parent.Torso.CFrame = script.Parent.Torso.CFrame + Vector3.new(50, 0, 30)

Answer this question