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

How do i use Cframes and loop tweens?

I need help on looping tweens and using Cframes as i am a newbie scripter, and need some help.

1 answer

I will go through the codes with explanations.

local TweenService = game:GetService("TweenService")--Include the service.
local DelayTime = .5--The delay time between the looped tween.

local CF1 = CFrame.new(0,0,1)
local CF2 = CFrame.new(1,1,1)
local CFrameValue = Instance.new("CFrameValue")

First, we define two CFrames -- CF1 and CF2. And a proxy value object CFrameValue since ROBLOX's TweenService can only be used on objects.

local T_Info = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

Second, we define our TweenInfo.

local TweenTable = {}--We only define it once, so we don't need to define it every loop.
while true do
	CFrameValue.Value = CF1--Start at CF1, value = CF1
	TweenTable.Value = CF2--Goal is CF2, table.CFrame = CF2
	--[[Note: Since CFrameValue object's CFrame value is held in the Value, so
	our table define it as Value.]]--
	local Tween1 = TweenService:Create(CFrameValue,T_Info ,TweenTable)
	--Remember the Create function returns a tween, not playing it.
	Tween1:Play()
	--Tween1.Completed:Wait()
	wait(DelayTime)
	CFrameValue.Value = CF2
	TweenTable.Value = CF1
	local Tween2 = TweenService:Create(CFrameValue,T_Info ,TweenTable)
	Tween2:Play()
	--Tween2.Completed:Wait()
	wait(DelayTime)
end

Put all together:

local TweenService = game:GetService("TweenService")--Include the service.
local DelayTime = .5--The delay time between the looped tween.

local CF1 = CFrame.new(0,0,1)
local CF2 = CFrame.new(1,1,1)
local CFrameValue = Instance.new("CFrameValue")

local T_Info = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local TweenTable = {}--We only define it once, so we don't need to define it every loop.
while true do
	CFrameValue.Value = CF1--Start at CF1, value = CF1
	TweenTable.Value = CF2--Goal is CF2, table.CFrame = CF2
	--[[Note: Since CFrameValue object's CFrame value is held in the Value, so
	our table define it as Value.]]--
	local Tween1 = TweenService:Create(CFrameValue,T_Info ,TweenTable)
	--Remember the Create function returns a tween, not playing it.
	Tween1:Play()
	--Tween1.Completed:Wait()
	wait(DelayTime)
	CFrameValue.Value = CF2
	TweenTable.Value = CF1
	local Tween2 = TweenService:Create(CFrameValue,T_Info ,TweenTable)
	Tween2:Play()
	--Tween2.Completed:Wait()
	wait(DelayTime)
end

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