Advanced typewriter

Hello everyone, I’ve been trying to achieve a falling down text effect recently to implement into my dialogue system.
Here’s a reference of what I want to make :


And this is what I made (it doesn’t work at all)

Heres the code for it :

local TextService = game:GetService("TextService")
local TweenService = game:GetService("TweenService")

local UI = script.Parent
local Frame = UI.TextFrame
local TextDefault = script:WaitForChild("Letter")

local X, Y = 0, 0
local TotalX = 0

local LastLetter : TextLabel = nil

local function TypeLetter(Letter : string)
	if TotalX >= Frame.AbsoluteSize.X then Y += 1 TotalX = 0 X = 0 LastLetter = nil end
	
	local Clone = TextDefault:Clone()
	Clone.Text = Letter
	local Position
	if LastLetter ~= nil then
		Position = UDim2.new(0, LastLetter.Position.X.Offset + TextService:GetTextSize(Letter, 28, Clone.Font, Clone.AbsoluteSize).X, Y * Clone.Size.Y.Scale)
	else
		Position = UDim2.new(0, 0, Y * Clone.Size.Y.Scale)
	end
	
	Clone.Position = Position - UDim2.fromScale(0, 0.05)
	Clone.Name = Letter
	Clone.Parent = Frame
	
	TweenService:Create(Clone, TweenInfo.new(.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {
		Position = Position,
		TextColor3 = Color3.fromRGB(139, 139, 139)
	}):Play()
	
	X += 1
	TotalX = Position.X.Offset
	
	LastLetter = Clone
end

game.ReplicatedStorage.Event.Event:Connect(function(Sentence : string)
	for i = 1, #Sentence do
		TypeLetter(string.sub(Sentence, i, i))
		task.wait(.05)
	end
end)

The TextDefault textlabel properties :


Thanks beforehand.

In the line with “Clone.Position = Position - UDim2.fromScale(0, 0.05)” aren’t you pushing the letter back?

2 Likes

No, I’m pushing it up so the tween can put it in the normal position.

1 Like

oh yh mb, not so familiar with UDim2

2 Likes

Could someone help me out please?

1 Like

The issue is that you’re not factoring size into the letter position! If you set the AnchorPoint of Letter to 0.5, 0 – this code will work:

local TextService = game:GetService("TextService")
local TweenService = game:GetService("TweenService")

local UI = script.Parent
local Frame = UI.TextFrame
local TextDefault = script:WaitForChild("Letter")

local X, Y = 0, 0
local TotalX = 0

local LastLetter : TextLabel = nil

local function TypeLetter(Letter : string)
	if TotalX >= Frame.AbsoluteSize.X then Y += 1 TotalX = 0 X = 0 LastLetter = nil end

	local Clone = TextDefault:Clone()
	Clone.Text = Letter
	Clone.Parent = Frame

	local letterSize = TextService:GetTextSize(Letter, 28, Clone.Font, Clone.AbsoluteSize)
	Clone.Size = UDim2.new(0, letterSize.X, Clone.Size.Y.Scale, 0)

	local Position
	if LastLetter ~= nil then
		local lastLetterPos = LastLetter.Position.X.Offset + (LastLetter.Size.X.Offset / 2)
		Position = UDim2.new(0, lastLetterPos + (letterSize.X / 2), Y * Clone.Size.Y.Scale)
	else
		Position = UDim2.new(0, letterSize.X / 2, Y * Clone.Size.Y.Scale)
	end

	Clone.Position = Position - UDim2.fromScale(0, 0.05)
	Clone.Name = Letter

	TweenService:Create(Clone, TweenInfo.new(.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {
		Position = Position,
		TextColor3 = Color3.fromRGB(139, 139, 139)
	}):Play()

	X += 1
	TotalX = Position.X.Offset + (Clone.Size.X.Offset / 2)

	LastLetter = Clone
end

game.ReplicatedStorage.Event.Event:Connect(function(Sentence : string)
	for i = 1, #Sentence do
		TypeLetter(string.sub(Sentence, i, i))
		task.wait(.05)
	end
end)

With this, I am just adding letterSize.X/2 to the last letter position :slight_smile:

Hope this pushes you in the right direction! Good luck!

2 Likes

Thanks a lot, now the only thing I gotta do is to fix the wording issue which probably will be easy (I hope saying that will make it easy)

2 Likes

By the way, any idea how I could make this work with TextScaled?

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.