How can I improve this?

What the code does

  • The code generates multiple text labels that create a piece of text. Multiple functions can be used to change the properties of the letters, such as making their colors change by the colors of the rainbow or making the letters bounce in a motion where each letter bounces after the one before does.

What I’m not satisfied with

  • The code makes it hard to position the text where I want it.
  • When switching to another window, outside of the studio, the bouncing function stops working (or at least the tween), and some or all of the letters bounce together instead of following the last, or they are just doing their own thing.
  • The offset variables are a bit confusing, and I also want to make the code look cleaner.

Potential improvements I considered

Making each letter more accessible through the letter table
Fixing the window-switching issue with the bounce function.
Rework the rainbow function so that the tween and “while task.wait() do” are in one easy-to-read function.
Make the code cleaner.

local screenGui = script.Parent
local text = "Wave" -- The text generated in a wavy format

local letters = {} -- A table containing each letter

local tweenService = game:GetService("TweenService")

local textLabelProperties = { -- The properties of each letter
	BackGroundColor = Color3.new(1, 1, 1);
	BackgroundTransparency = 0;
	Size = UDim2.new(0.05, 0, 0.1, 0);
	Color = Color3.new(0, 0, 0);
	Font = Enum.Font.GothamBold;
	Weight = Enum.FontWeight.ExtraBold;
	TextScaled = true;	
}

letterFunctions = { -- Functions that can be used to animate the textures
	rainbowTween = function(textLetter : TextLabel, color : Color3) -- Tweens the color of the text in a rainbow.
		tweenService:Create(
			textLetter,
			TweenInfo.new(0.25, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0), {
				TextColor3 = color
			}
		):Play()
	end;
	
	bounceTween = function(textLetter : TextLabel) -- Tweens the position of the text and makes it bounce.
		tweenService:Create(
			textLetter,
			TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, -1, true, 0), {
				Position = textLetter.Position + UDim2.new(0, 0, 0.05, 0)
			}
		):Play()
	end;
}

local offset = 0.05 -- The distance between each letter
for i = 1, #text do
	local letter = Instance.new("TextLabel")
	letter.Name = "Letter"
	letter.BackgroundColor3 = textLabelProperties.BackGroundColor
	letter.BackgroundTransparency = textLabelProperties.BackgroundTransparency
	letter.BorderSizePixel = 0
	letter.Text = string.sub(text, i, i)
	letter.Size = textLabelProperties.Size
	letter.TextColor3 = textLabelProperties.Color
	letter.Font = textLabelProperties.Font
	letter.FontFace.Weight = textLabelProperties.Weight
	letter.TextScaled = true
	
	letter.Position += UDim2.new(offset, 0, 0.4, 0) -- Sets the position of the letter with the given offset
	offset += 0.05 -- Increases the offset of the letter so that each is spaced out
	
	letter.Parent = screenGui
	letters[letter] = letter -- Adds the letter to the table
	
	task.spawn(function()
		letterFunctions.bounceTween(letter)
		
		while task.wait() do
			local hue = tick() %  1.75 / 1.75
			local color = Color3.fromHSV(hue, 1, 1)

			letterFunctions.rainbowTween(letter, color)
		end
	end)
	
	task.wait(0.1)
end

Here I would just say you should clean the code, there is some clutter as you mentioned but that’s all

Do you mean, as in space it out? Be more specific

1 Like