Help With CFrame

Hello developers!

I’ve personally never used CFrame, and plan on learning after this, but I need to do something, and I don’t know how. I need to make a block move up for 3 seconds, and then down for 3 seconds, and repeat.

Thanks,
Ben

1 Like

Hello!

You can move a instance by taking its current CFrame and moving it by 3 studs on the Y axis like so:

Part.CFrame = Part.CFrame * CFrame.new(0, 3, 0)
--Pretty sure this works as well, correct me if I am wrong.
Part.CFrame *= CFrame.new(0, 3, 0)
--This also applies to moving it down ways by using a negative value instead.

--Position works as well
Part.Position += Vector3.new(0, 3, 0)

--Sorry didn't read the entire question correctly, here is how you would tween the parts position:
local TweenService = game:GetService("TweenService")
while true do
	TweenService:Create(Part, TweenInfo.new(3), {Position = Vector3.new(0, 3, 0)}):Play()
	wait(3)
	TweenService:Create(Part, TweenInfo.new(3), {Position = Vector3.new(0, 0, 0)}):Play()
	wait(3)
end
3 Likes

He said 3 seconds, not studs

There are lots of ways to do this but the easiest is to probably just use TweenService to tween the CFrame

It’s working well, except it moves the part.

You said within your question your trying to move a part? Is there something else too?

I’m trying to move it up and down, yet it goes to the right.

The code I provided moves the part on the Y axis as show here: Vector3.new(0, 3, 0)
Make sure to check your values.

CFrame is essentially just a more useful and powerful version of “Position” with some extra benefits. The basic CFrame constructor “CFrame.new(vector3pos , vector3lookAt)” is a very useful way of moving things and setting the rotation of them. A very good tutorial I recommend is @Alvin_Blox’s “What Is CFrame?” (What Is CFrame? | Roblox CFrame Tutorial | LookVector, Angles & More! - YouTube). Regarding your question about part movement, try this:

local TweenService = game:GetService("TweenService") --Get the Tween Service (basically a part animator)

local part = script.Parent

local tweenInfo = TweenInfo.new(
	
	3, --the duration of the tween in seconds
	Enum.EasingStyle.Quad, --the easing style of the tween (https://developer.roblox.com/en-us/api-reference/enum/EasingStyle)
	Enum.EasingDirection.Out, --the aeasing direction of the tween (https://developer.roblox.com/en-us/api-reference/enum/EasingDirection)
	-1, --the amount of times the tween will repeat (-1 is indefinite)
	true, --whether or not the tween will reverse
	.5 --delay between reverses
	
)

local partGoal = {
	CFrame = part.CFrame * CFrame.new(0,9,0) --This is making it so that the part moves 9 studs in the Y direction, relative to it's current CFrame
}

local partTween = TweenService:Create(part,tweenInfo,partGoal)
partTween:Play()