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

part rotation and moving not working?

I want to rotate the part on the x axis and move it up and down on the y axis but it seems to only do one or the other

while true do wait()
	script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)

	for i = 1,100 do
		script.Parent.CFrame = CFrame.new(script.Parent.Position + Vector3.new(0,1,0))
		wait()
	end
	for i = 1,100 do
		script.Parent.CFrame = CFrame.new(script.Parent.Position - Vector3.new(0,1,0))
		wait()
	end
end

1 answer

✓ Accepted answer

The only time you rotate the part is before you move it up and down and you only rotate it once (0.1 radians in the y axis). So you have to rotate it while it's going up/down, meaning you have to rotate it inside the for loops.

Your code should look like this:

while true do wait()
    
    for i = 1,100 do
        script.Parent.CFrame = CFrame.new(script.Parent.Position + Vector3.new(0,0.1,0))
        script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)
        wait()
    end
    for i = 1,100 do
        script.Parent.CFrame = CFrame.new(script.Parent.Position - Vector3.new(0,0.1,0))
        script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)
        wait()
    end
end

However, if you test this, you will see that the part looks glitchy, where it tries to rotate but does not. This is because when you use CFrame.new() you're creating a completely new CFrame, so any rotation would be removed.

This is the line I'm referring to:

script.Parent.CFrame = CFrame.new(script.Parent.Position + Vector3.new(0,0.1,0))

What you want to do when setting the position is this: script.Parent.CFrame = script.Parent.CFrame - Vector3.new(0, 0.1, 0) instead of CFrame.new(script.Parent.Position - Vector3.new(0,0.1,0))

So your code should look like this:

while true do wait()
    
    for i = 1,100 do
        script.Parent.CFrame = script.Parent.CFrame - Vector3.new(0, 0.1, 0)
        script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)
        wait()
    end
    for i = 1,100 do
        script.Parent.CFrame = script.Parent.CFrame - Vector3.new(0, 0.1, 0)
        script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)
        wait()
    end
end

Also, you can improve your code by storing script.Parent in a variable to make your code more readable:

local part = script.Parent

while true do wait()
    for i = 1,100 do
        part.CFrame = part.CFrame - Vector3.new(0, 0.1, 0)
        part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)
        wait()
    end
    for i = 1,100 do
        part.CFrame = part.CFrame - Vector3.new(0, 0.1, 0)
        part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0)
        wait()
    end
end

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