Better way to do tweening?

I have minimal experience in tweenservice and made this script for an NpcChatGUI n’ I feel like its a bit long so I wondered if there was a way to compress it down a bit?

		script.Parent.TextLabel:TweenSizeAndPosition(UDim2.new(0, 540,0, 50),UDim2.new(0.5, 0,0.613, 0), nil, nil, 1)
		
		local tween1 = tweenService:Create(script.Parent.TextLabel, tweenInfo1, {TextTransparency = 0})
		local tween2 = tweenService:Create(script.Parent.TextLabel, tweenInfo1, {BackgroundTransparency = 0.25})
		local tween3 = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1, {BackgroundTransparency = 0.25})
		local tween4 = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1, {TextTransparency = 0})	
		
		tween1:Play()
		tween2:Play()
		wait(2)
		tween3:Play()
		tween4:Play()
		
		script.Parent.TextLabel.NPCtalking:TweenSizeAndPosition(UDim2.new(0, 530,0, 225),UDim2.new(0.499, 0,3.462, 0))

---Random codiing stuff here blah blah

	local tween1exit = tweenService:Create(script.Parent.TextLabel, tweenInfo2, {TextTransparency = 1})	
	local tween2exit = tweenService:Create(script.Parent.TextLabel, tweenInfo2, {BackgroundTransparency = 1})
	local tween3exit = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1, {BackgroundTransparency = 1})
	local tween4exit = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1, {TextTransparency = 1})	
	print("Running exit animation")
		script.Parent.TextLabel.NPCtalking:TweenSizeAndPosition(UDim2.new(0, 549,0, 10),UDim2.new(0.499, 0,0.892, 0), nil, nil, 0.8)
	tween3exit:Play()
	tween4exit:Play()
			wait(0.5)
	tween1exit:Play()
	tween2exit:Play()
		script.Parent.TextLabel:TweenSizeAndPosition(UDim2.new(0, 0,0, 0),UDim2.new(-1, 0,0.613, 0), nil, nil, 3.5)

Is there a better way to reduce the amount lines needed? (Yes, I know I should’ve turns script.Parent.TextLabel into a local variable but since Im planning to revamp the whole thing anyway I didnt bother to change it)

In my opinion, if the script works, it doesn’t matter how long the script is.

Well Im in Code Review for a reason lol, Im planning to revamp it and want to make it easier for myself than how Im currently doing it

1 Like

Your script is okay.

1 Like

Where’s “tweenService”???

Summary

This text will be hidden

tweenService is defined already, just in a part of the code that’s I dont intend to remake. Assumed I wouldn’t need to put it in since its probably obvious that’s its already defined as the script works

image

1 Like

You’re using two separate tweens to tween different properties on the same instance, over the same duration of time. Perhaps consider grouping TextTransparency and BackgroundTransparency together, into a single tween.

TweenService:Create(TextLabel, TweenInfo, {BackgroundTransparency = quantity, TextTransparency = quantity})

That should help a bit, but honestly I’d prefer to write my own interpolation algorithm, so that I could handle the animation in a single thread, as opposed to a million tweens.

2 Likes

You Can Change To Something Like

script.Parent.TextLabel:TweenSizeAndPosition(UDim2.new(0, 540,0, 50),UDim2.new(0.5, 0,0.613, 0), nil, nil, 1)
		
		local tween1 = tweenService:Create(script.Parent.TextLabel, tweenInfo1, {TextTransparency = 0; BackgroundTransparency = 0.25})
		local tween3 = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1, {BackgroundTransparency = 0.25; TextTransparency = 0})	
		
		tween1:Play()
		wait(2)
		tween4:Play()
		
		script.Parent.TextLabel.NPCtalking:TweenSizeAndPosition(UDim2.new(0, 530,0, 225),UDim2.new(0.499, 0,3.462, 0))

---Random codiing here

	local tween1exit = tweenService:Create(script.Parent.TextLabel, tweenInfo2, {TextTransparency = 1; BackgroundTransparency = 1})
	local tween3exit = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1, {BackgroundTransparency = 1; TextTransparency = 1})	
	print("Running exit animation")
		script.Parent.TextLabel.NPCtalking:TweenSizeAndPosition(UDim2.new(0, 549,0, 10),UDim2.new(0.499, 0,0.892, 0), nil, nil, 0.8)
	tween3exit:Play()
			wait(0.5)
	tween1exit:Play()
		script.Parent.TextLabel:TweenSizeAndPosition(UDim2.new(0, 0,0, 0),UDim2.new(-1, 0,0.613, 0), nil, nil, 3.5)

Seems like one of my creations may kind of help you

function StartTween(Object,Time,Type,Value,Placement,Name)
	-- Reminder on EasingStyles (Type Value) --
	-- Linear								 --
	-- Sine									 --
	-- Back									 --
	-- Quad									 --
	-- Quart								 --
	-- Quint								 --
	-- Bounce								 --
	-- Elastic								 --
	-- Exponential							 --
	-- Circular								 --
	-- Cubic 								 --
	-------------------------------------------
	if Type == nil then
		Type = Enum.EasingStyle.Linear -- sets to default type
	end
	local Tween = game:GetService("TweenService"):Create(Object, TweenInfo.new(Time,Type),Value)
	Tween:Play()	
	if Placement == nil then
	elseif Placement ~= nil and type(Placement) == type(script) then
		Tween.Parent = Placement
	end
	if Name == nil then
		
	elseif Name ~= nil and type(Name) == type("") then
		Tween.Name = Name
	end
end

-- Example --
StartTween(script.Parent,2,Enum.EasingStyle.Sine,{Position = UDim2.new(1,0,1,0)},nil,nil)
-------------

Original Model creation : Gui functions | (feedback topic)

1 Like

Tweens can handle multiple properties at once. Since tween1 & 2 are operating on the same part, for the same duration, and fired at the same time, they can be combined. Same with 3 & 4…

local tween1n2 = tweenService:Create(script.Parent.TextLabel, tweenInfo1, {
  TextTransparency = 0,
  BackgroundTransparency = 0.25
})
local tween3n4 = tweenService:Create(script.Parent.TextLabel.NPCtalking, tweenInfo1 {
  BackgroundTransparency = 0.25,
  TextTransparency = 0
})	
		
		tween1n2:Play()
		wait(2)
		tween3n4:Play()
1 Like

This is horrible advice and will mislead him down terrible code base paths.

Tweens can take multiple properties at the same time. Like @YosemiteMe said.

1 Like