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

How do I stop objects from rotating while CFraming?

I'm trying to make a script that moves a train forward. I'm still pretty new to CFraming. Currently what happens is the train moves in the right direction but some parts get rotated a different way then they originally are. Here's my script.

while true do
		wait(.0001)
	for _,v in pairs(game.Workspace.Train:GetChildren()) do
		v.CFrame = CFrame.new(v.CFrame.X + -.3, v.CFrame.Y, v.CFrame.Z)
	end 
	end

1 answer

✓ Accepted answer

#What you could do? When you change the CFrame of an object you change its Position as well as its Rotation. You need to change the rotation at the same time as you change the position. A tip is when you're doing a while loop you can put a wait instead of true so that after every wait it will loop. You can also write down workspace by itself instead of game.Workspace.


#Angles In CFrame you get a 3D position and rotation. When you just set the position the rotation is 0 by default.

part.Rotation = Vector3.new(0,102,2) --if the argument is left blank the script assumes it's a 0, so the full coordinates are 0,102,2; Rotation is set
part.CFrame = CFrame.new(0,10) --Same thing with the blank argument.
--The part's rotation is reset.

There is a simple way to set the Angle's rotation: CFrame.Angles! This Constructor will return a rotation that you can combine with the CFrame to get a position AND rotation.

part.CFrame = CFrame.new(0,2,0)*CFrame.Angles(math.pi,0,0) --You need all 3 arguments in order to combine them, also math.pi is 180 degrees in angles. So pi/2 is 90 degrees and pi*2 is 360 degrees.

#Final Product I'll record the original rotation via variable and apply it later.

while wait() do --The smallest time you can make a wait is 0.003, it's default.
    for _,v in pairs(workspace.Train:GetChildren()) do
	local rot = v.Rotation
	local x,y,z = rot.X,rot.Y,rot.Z
        v.CFrame = CFrame.new(v.CFrame.X + -.3, v.CFrame.Y, v.CFrame.Z)*CFrame.Angles(math.rad(x),math.rad(y),math.rad(z))
    end --math.rad changes the number into radians or degrees so it's compatible with CFrame.Angles
end


Hope it helps!

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