Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make a part always stay on the same Y?

I've already tried it like this:

	if script.Parent.Position.Y ~= -20.44 then
		script.Parent.Position.Y = -20.44
	end

and this:

while true do
	if script.Parent.Position.Y ~= -20.44 then
		script.Parent.Position.Y = -20.44
	end
wait(1)
end

Neither of them work, and I'm clueless on what to try next.

3 answers

✓ Accepted answer

Position is a Vector value, and when you edit a Vector value, you have to edit all parts of the Vector. What you're trying to do seems to me like you want the Y Position of the Brick to constantly be at -20.44. Well the easiest way to do that would be like this:

local Part = script.Parent
while true do
	Part.Position = Vector3.new(Part.Position.x, -20.44, Part.Position.z)
	--Basically this keeps the Part's X and Z positions the same, but sets the Y position to -20.44
	wait(1)
end

Hope this helped!

NOTE: For more info on Vectors, click here: Vectors

1 comment

0

Thanks, that worked! I just didn't know how to put Vector3.new with just a Y value. I didn't think to do what you did at all.

First of all, you can realize that the if is actually unnecessary, since setting the value to what it was won't change it.

The if will continue-on most of the time anyway, because strange decimals can't usually be compared reliably with == or ~=.


The Position property is a Vector3. If we look at that Wiki article and look at Vector3's properties you'll see that they are all read only.

That means you aren't allowed to set (write to) a Y, only get (read from) it.


Instead, we'll have to construct a new position at the same place but with a different Y.

We can do that in essentially two basic ways:

newpos = Vector3.new( oldpos.x , -20.44, oldpos.z )
-- OR
newpos = Vector3.new(1,0,1) * oldpos + Vector3.new(0, -20.44, 0)

We can just write to a part's Position with this, but modifying the Position property can have bad effects. Instead, we should probably write to its CFrame property.

We could make a new CFrame object from the newpos but then if the part was rotated, it would unrotate.

So we'll use the starting CFrame, but subtract the oldpos and add the newpos to put it in the right place:

newcframe = oldcframe - oldpos + newpos


Or, instead of using a script to do this, you could use ROBLOX's physics engine, via the BodyPosition object. If the maxForce property only has a Y component, then it will only constrain the object's Y position.

(This only works if the script is inside the part...)

Try something like...

Part = script.Parent
Y = -20.44

while wait() do
Part.CFrame = CFrame.new(Part.CFrame.X, Y, Part.CFramt.Z)
end

I don't know what the Y Axis is that you wanted the part at, but assuming it's the same as from your script, I set it to 20.44.

Also, I used CFrame, but it'd work the same with Position...

Well, I guess this isn't really helpful, but bye.

  • Kratos232

This discussion is preserved as it was. New answers, comments, and votes are closed.