I want my model to always stay in a specific Z

I have a train which moves in a straight line. I want the train to stay in the same Z Coordinates. This is my code



local train = script.Parent.Parent


while true do
	local cframeValue = train:GetPivot()
	local x = cframeValue.Position.X
	local y = cframeValue.Position.Y
	local z = cframeValue.Position.Z
	train:PivotTo(CFrame.new(x, y, 139.115))
	wait(0.0000001)
end


However when the train is moving, it extremely un-smooth (if thats a word). Is there a better way to script this. Oh and the train is physics based

Why are you CFraming a physics based train?
There are a few posts about making physics based trains. If it’s travelling in a straight line you can use a PrismaticConstraint to move it smoothly.

Also, when you need a script to wait you should use task.wait() instead of wait(0.0000001).
I believe it’s equivalent to 1/30th of a second (1 frame) when there is no lag present.
You have wait(0.0000001) but since task.wait() equals .03333 seconds you just cant go lower than that anyway.

it is travelling in a straight line so i will use prismatic constraints?

is it possible for me to use prismatic constraints even though the train has to travel on seperate track pieces

It’s smooth and the train won’t derail. You can change the speed of the PrismaticConstraint as well as the MotorMaxAcceleration to give it more realistic acceleration.

If it’s a straight line then the track and wheels don’t even have to touch. You can have a million track parts and the train will follow the PrismaticConstraint line.

If you want it to move smoothly frame by frame, you should consider hooking this up to an event within the RunService. Otherwise, you should look into body movers and motors.

PlatformModel - Roblox here’s a model I made for a moving platform for someone who hadn’t used PrismaticConstraints before.
You can weld your entire train to the ‘platform’ piece and it’s going to move in that straight line.
The script is just a simple control of the Prismatic properties. You’ll probably have to tweak the values in the Properties to get your train moving the way you want it to.

1 Like

Thanks a lot for the solution. I really appreciate it

1 Like

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