Making a tween once and playing it when needed VS. Making a tween whenever it needs to be played

I want to make the player’s camera look at a part whenever a button is pressed, and then bring the camera back to where it was when clicking another button. I made this snippet of code:

local Camera = game.Workspace.CurrentCamera

local TS = game:GetService("TweenService")
local playButtonTween = TS:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {CFrame = MapCFrame)
local playButtonTweenBack = TS:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {CFrame = DefaultCFrame)

local Main = script.Parent
local playButton = Main.Play
local exitButton = Main.Exit

playButton.MouseButton1Down:Connect(function()
	playButtonTween:Play()
end)

exitButton.MouseButton1Down:Connect(function()
	playButtonTweenBack:Play()
end)

My brain tells me that this code is much more efficient than the next one because it creates a tween once and plays it when needed, instead of creating a new tween everytime it needs to be played.

local Camera = game.Workspace.CurrentCamera

local TS = game:GetService("TweenService")

local Main = script.Parent
local playButton = Main.Play
local exitButton = Main.Exit

playButton.MouseButton1Down:Connect(function()
	local Tween = TS:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {CFrame = MapCFrame})
	Tween:Play()
end)

exitButton.MouseButton1Down:Connect(function()
	local Tween = TS:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {CFrame = DefaultCFrame})
	Tween:Play()
end)

However, whenever I look up a tutorial of tweening, or examples of a tween script, everybody seems to be using the second method. I have checked the developer hub and there is nothing relevant to this there.

Am I missing something here? Is there even a significant performace/memory impact when using the latter? Please help, I’m very confused by this

You’re not missing anything, and there isn’t a significant impact when using the latter. The former is more efficient.

1 Like