How can I change a single coordinate of a character's position?

Hi all, extremely new developer here and I have what is almost definitely an easy problem I was hoping someone can help with.

I’m creating a teleporter script that moves a player from one side of the map to the other. It’s a large wall so I want to keep the Y and Z coordinates of the character and only change their X coordinate. I cannot find the proper syntax to make it work. Here is what I have right now.

	local character = player.Character
	local rootPart = character:FindFirstChild("HumanoidRootPart")
	local rootPos = rootPart.CFrame.Position
	
	rootPos = Vector3.new(-370.5, rootPos.Y, rootPos.Z)
1 Like

In your script, you’re creating a variable by doing this:

You modify rootPos, your variable later in the script. Unlike some other value types, CFrame and Vector3 are not referenced but are, instead, copied when you create a local variable. You also cannot modify a Vector3 inside of a CFrame. The code would need to be:

rootPart.CFrame = CFrame.new(-370.5, rootPos.Y, rootPos.Z)

This will set the rootPart’s CFrame to the position you specify.

Thank you. The explanation about CFrame and Vector3 helps a lot of issues I have.

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