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 )
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.
1 comment
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.