How to change position of part on one axis?

I just want to change the Y position of a part.

I used this:

maskPart.Position.Y = 31

It prints out error saying “y cannot be assigned to”

why?

1 Like

I will be honest and tell you I dont know why…

But you can change the y using

vector3.new(Position.X,31,Position.Z)
4 Likes

It’s because Vector3’s (position and size properties of parts), among many other Roblox datatypes, aren’t mutable. You have to create an entirely new Vector3 to assign to the property.

2 Likes

I’m just changing the Y value so it isn’t a vector 3.

You’ll need to create a new vector3:

--maskPart.Position.Y = 31 

--Try this instead:
maskPart.Position = maskPart.Position + Vector3.new(0, 31, 0)
-- or
maskPart.Position += Vector3.new(0, 31, 0)
8 Likes

It’s not a vector 3 I’m just changing the Y value (Inside of the position property tab.) I did try using a Vector3 and even still it printed the same error.

Right but you can’t change a single axis (which would be mutating the Vector3), you have to create a whole new Vector3 to assign to the part’s Position property.

Try the code I wrote. If you want to move it up you need to add a vector3 to the vector3 of the part.

well yeah I already did try that many times but it printed out the same error saying “Y cannot be assigned to”

Or you can add to the part’s Vector3 position using another Vector3.

Edit:

Cody is correct, using the + operator on two vectors creates a new vector internally.

That’s still creating a new Vector3 though.

1 Like

That’s not the error from my code. My code doesn’t reference the Y property. Try replacing all of the old code.

I did already. This is the code I have:

if value == false then
	maskPart.Position = Vector3.new(11.25, 31, -1)
else
	maskPart.Position = Vector3.new(11.25, 9, -1)
end

That code doesn’t throw an error. Try sending the line that throws the error.

Whats wrong with chaning the y-Value by creating a Vector3?

Basically I can create a new part position by taking its x,z and putting it into a new Vector

Vector3.new(x,9,z)

is this not what your trying to do?
I can create a function for you hopefully that helps

function SetPartPosition(Object,yValue)
     return Object.Position = Vector3.new(Object.Position.X,yValue,Object.Position.Z)
end

function AddPartPosition(Object,yValue)
     return Object.Position -= Vector3.new(0,yValue,0)
end
--You can call these functions like this:
local Part = game.Workspace.part
SetPartPosition(Part,9)
AddPartPosition(Part,22)

It’s because you can’t specifically change one axis of a part’s position

This is what I did:

local goalPos = Vector3.new(100, 5, 100) -- your goal position
local x = goalPos.X - origin.X
local z = goalPos.Z - origin.Z
local newPosition = origin + Vector3.new(x, 0, z)