Dialogue - Typewriter System

(3) Dialogue Example - Roblox

So this code types out any message in a function named ‘DisplayMessage’. It creates a textlabel for every piece of the message, positions it upwards then tweens it down for a smooth looking effect.

The reason I want this code reviewed, is because I think I’m doing something inefficiently. I’d like to know if there’s an alternative to the ‘positioning’ of each textlabel for X. If you click test in the game you’ll notice sometimes the texts space over or a bit into eachother.

I do believe the game is private, unless it’s not finished yet, I would make it public. I’m guilty of doing this myself. :sweat_smile:

Sorry bout that, should be public now. Thanks for the heads up.

Lil wonky.

Yeah, I was wondering if there’s an alternative method to making it less wonky. All I’m doing is adding to a variable each time a letter is typed down.

Not entirely sure how to help your case particularly, but I did make an old module that used GetTextSize; I recommend you look in to it.

This is some old code, don’t judge me too hard.

local textService = game:GetService("TextService")
local SplitTextBox = {}

function SplitTextBox:Split(textBox)
	local holder = Instance.new("Frame")
	holder.Name = textBox.Name
	holder.Parent = textBox.Parent
	holder.Size = textBox.Size
	holder.Position = textBox.Position
	holder.BackgroundTransparency = 1
	holder.ClipsDescendants = true
	
	local text,textColor,textSize,font,y,ypos = textBox.Text, textBox.TextColor3, textBox.TextSize, textBox.Font, textBox.Size.Y.Scale, textBox.Position.Y.Scale
	textBox:Destroy()
	local x = 0
	local textLabels = {}
	for i = 1, #text, 1 do
		local character = string.sub(text,i,i)
		local textLabel = Instance.new("TextLabel")
		textLabel.Text = character
		textLabel.TextColor3 = textColor
		textLabel.TextSize = textSize
		textLabel.Font = font
		local neededSize = textService:GetTextSize(character, textSize, font, textLabel.AbsoluteSize)
		textLabel.Size = UDim2.new(0, neededSize.X, 0, neededSize.Y)
		textLabel.TextScaled = true
		textLabel.BackgroundTransparency = 1
		textLabel.AnchorPoint = Vector2.new(0,0.5)
		textLabel.Position = UDim2.new(0, x, 0.5, 0)
		textLabel.Parent = holder
		x = x + neededSize.X
		table.insert(textLabels, textLabel)
	end
	return holder, textLabels
end

return SplitTextBox

I’m not too familiar with ‘GetTextSize’, but I tried doing what it said on the wiki while kind of referencing off of the code you sent. Long story short, it’s a lot more wonkier than it used to be.

local textService = game:GetService("TextService")
local x = 0
local y = 0
function typeLetter(Letter)
	local TL = script:WaitForChild("Letter"):Clone()
	TL.Text = Letter
	TL.Parent = script.Parent:WaitForChild("DescFrame")
	local textSize,font = TL.TextSize, TL.Font
	local neededSize = textService:GetTextSize(Letter, textSize, font, TL.AbsoluteSize)
	x = x + neededSize.X
	print(neededSize.X, Letter)
	TL.Position = UDim2.new(0, x, y - 0.1, 0)
	TL:TweenPosition(UDim2.new(0,x,y,0),"Out", "Quad", .1, true)
	if x > 200 then
		y = y + .4
		x = 0
	end
	if Letter == "." or Letter == "!" or Letter == "?" then
		wait(.5)
	end
end