Is it possible to use tweenservice on a CFrame variable?

I’m trying to use tween service to create more complex effects that cannot be achieved using something like lerp (or to have to option to easily switch between many easing styles).

I have one model, which has multiple cframes being multiplied on it every frame, so I can’t put it on the model itself.

I need to create a dequipping and equipping animation, and want to use the easing style elastic because it looks good.

I need to use it on a variable but it doesn’t look like I can, but if I can I really have no idea how.

I don’t want to create an object in the 3d space to apply the tween on, then get the cframe of the object and multiply it onto the model, because that seems incredible inefficient.

Does anyone know if I can do this? Thanks.

10 Likes

There are two ways you can go about this:

  1. You can use TweenService:Create on a CFrameValue:
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local cf = Instance.new("CFrameValue")
local tween = TweenService:Create(
	cf,
	TweenInfo.new(--[[stuff here]]), 
	{Value = CFrame.new(0, 0, 1)}
)

tween:Play()
local conn = RunService.RenderStepped:Connect(function(dt)
	-- do stuff with cf.Value while it's being tweened
end)

tween.Completed:Connect(function()
	if conn then
		conn:Disconnect()
	end
end)
  1. You can use TweenService:GetValue in a while loop:
local duration = 1

local cfStart = CFrame.new()
local cfGoal = CFrame.new(0, 0, 1)

local i = 0
while i < 1 do
	local dt = wait() -- or RunService.RenderStepped:Wait()

	--[1] do
	i = math.min(i + dt/duration, 1)
	local a = TweenService:GetValue(i, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local cfTween = cfStart:Lerp(cfGoal, a)
	-- end

	-- you can also increment multiple other tweens here with similar code from [1]

	-- do stuff with cfTween here
end
36 Likes

thank you for this :heart:
I completely forgot CFrame values existed…
I think I’m going to use the top option for simplicity sake but this fixes my problem :slight_smile:

3 Likes

Apologies for reviving an old thread. Would you mind explaining how and what GetValue does as there’s very little on the documentation and non elsewhere.

4 Likes

If you know what lerping is, this should be very simple to understand. I made an example here to help you figure out what that is:

In relation to this, a Tween is a representation of lerping values, but instead of having an arbitrary slider value to move a point by, this is replaced by a unit of time. This time unit is distorted by an easing function as determined by the Enum.EasingStyle you chose (like cubic) and applied as a lerp on all properties of the affected instance in relation to where it started (A) and the goal you passed in (B).

What TweenService:GetValue does is give you the freedom to choose what time unit you want (alpha), what easing function to distort it with (easingStyle), and how that easing function is applied (easingDirection). It then spits out a new alpha that can be applied to all the :Lerp functions you see on Vector3s, Color3s, UDim2s, and other data types.

5 Likes

Yes, you could use TweenInfo. An example is:

(note, i just realized that I replied to GoldenStein’s reply, my bad, this is a general reply)

TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) = {C1 = CFrame.new(1,1,1)}

Correct me if I’m wrong, but I’ve used this for a long while.

Hope I helped.

2 Likes