Issues with keeping rotation when teleporting player

Whenever i use this method the orientation from where i teleport is reversed, or randomized for some reason

plr.Character.PrimaryPart.CFrame = CFrame.new(MouseHit.X, MouseHit.Y + 4, MouseHit.Z) * CFrame.Angles(plrHRP.Rotation.X, plrHRP.Rotation.Y, plrHRP.Rotation.Z)

I’ve been trying to debug this for an hour now and i cannot find out why.

I really want to teleport the player and keep his orientation/rotation.

Reason why i didn’t move the humanoidRootPart is because it experienced some issues and glitched out the player for no reason and put him out of proportion.

2 Likes

Does this work for you?

-- Get the player and their current position and orientation
local player = game.Players.LocalPlayer
local currentPosition = player.Character.HumanoidRootPart.Position
local currentOrientation = player.Character.HumanoidRootPart.Orientation

-- Define the range of coordinates for the random teleport
local minX = -50
local maxX = 50
local minY = 5
local maxY = 20
local minZ = -50
local maxZ = 50

-- Generate random coordinates within the defined range
local newX = math.random(minX, maxX)
local newY = math.random(minY, maxY)
local newZ = math.random(minZ, maxZ)

-- Teleport the player to the new location while keeping orientation
player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(newX, newY, newZ), Vector3.new(currentOrientation.x, currentOrientation.y, currentOrientation.z))
2 Likes

Works but i cannot use humanoidrootpart cause it messes with the character. My friend said my character was put out of proportion. I used primarypart of the characters model and it’s still reversed for some reason

2 Likes

have you tried doing:

Character:MoveTo(position)?

2 Likes

For Models with a primarypart:

-- Get the model you want to move
local model = game.Workspace.YourModelNameHere

-- Define the target location
local targetPosition = Vector3.new(x, y, z) -- Replace x, y, and z with the desired coordinates

-- Move the model to the target location
model:SetPrimaryPartCFrame(CFrame.new(targetPosition))
3 Likes

Worked, it works well now thank you

2 Likes

Do you have a version that also keeps it’s rotation, i took wrong sorry

2 Likes
-- Get the model you want to move
local model = game.Workspace.YourModelNameHere

-- Define the target position and the current orientation of the model
local targetPosition = Vector3.new(x, y, z) -- Replace with desired target position
local currentOrientation = model.PrimaryPart.CFrame:toEulerAnglesXYZ()


-- Create a new CFrame with the target position and the original orientation
local newCFrame = CFrame.new(targetPosition) * CFrame.Angles(currentOrientation.x, currentOrientation.y, currentOrientation.z)

-- Apply the new CFrame to the model's primary part
model:SetPrimaryPartCFrame(newCFrame)

You could just refer to the previous code I sent, pretty much the same thing

2 Likes

Alright thanks man, appreciate it for taking your time

1 Like

I’m sorry but i am getting a error saying: Attempt to index number with X

local CurrentOrientation = plr.Character.PrimaryPart.CFrame:toEulerAnglesXYZ()
local newCFrame = CFrame.new(MouseHit.X, MouseHit.Y + 3, MouseHit.Z) * CFrame.Angles(CurrentOrientation.X, CurrentOrientation.Y, CurrentOrientation.Z)
1 Like

Overlooked that.
Assign it to 3 different variables then you can fix the rest.
local OrientX, OrientY, OrientZ = cf:ToEulerAnglesXYZ()

2 Likes

Works, Thanks G

30 msg limit aaaaaa

1 Like

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