Issue with character spacing in a custom Typewriter script

Basically, I made a module that separates each character of a string into it’s own TextLabel so I’m able to animate it later. The issue is that I don’t know how to fix the spacing between letters, because there’s too much on some (as you can see with “l” or “i”) and too little on others (like with “m” and “a”).


Any help would be appreciated.

2 Likes

You can use TextLabel.MaxVisibleGraphemes to animate it.

Example:

local label = PATHTOLABELHERE
local speed = .5
local increment = .5

local function animateLabel(speed : number, increment : number)
    if label.MaxVisibleGraphemes == 0 then
        task.spawn(function() 
            while wait(speed) do
                label.MaxVisibleGraphemes = label.MaxVisibleGraphemes+increment

               if label.MaxVisibleGraphemes == label.Text:len() then
                   break
               end
            end
        end)

    elseif label.MaxVisibleGraphemes == label.Text:len() then
        task.spawn(function() 
            while wait(speed) do
                label.MaxVisibleGraphemes = label.MaxVisibleGraphemes-increment

               if label.MaxVisibleGraphemes == 0 then
                   break
               end
            end
        end)
    end
end

animateLabel(speed, increment)

It has to be the same label for it to work.

1 Like

I don’t think you understood my issue, if you look at the video, you can see that for example in “make” or “doing” there are some weird spaces between the letters, that’s what I’m trying to get rid of.

1 Like

It’s probably from each separate TextLabel being spaced out too far. If you use one TextLabel you are able to control the spacing easier. Apologies for my lack of explanation.

1 Like

What about TextService and get text size?

My friend recently showed me something in messages and you want to animate it; local width = textservice:gettextsize().X

framesize can just be math.huge on vector2
TextService:GetTextSize(character, textSize, font, Vector2.new(math.huge, math.huge)).X

1 Like

That’s exactly what I’m using for this, I THOUGHT I could maybe use it individually for each character but it always return the same size, no matter the letter.

Here’s the code if it helps:

function TextModule.Write(Frame, Text)
	local TextSize = game:GetService("TextService"):GetTextSize(Text, TextFontSize, TextFont, Vector2.new(math.huge, math.huge))
	
	local char_size = TextSize.X/#Text
	
	local Separated_Text = separate_Text(Frame, Text, TextSize) --This returns an array of the text divided in several lines
	
	for _i, v in pairs(Separated_Text) do
		for i, v in pairs(Frame:GetChildren()) do
			if v.Name ~= "".._i then
				v.Position = v.Position + UDim2.new(0, 0, -.06, 0) --This moves all previous lines upwards
			end
		end
		
		for i=1,#v,1 do
			local Label = script.Example:Clone()
			Label.Parent = Frame
			Label.Name = _i
			
			Label.Size = UDim2.new(0, char_size, 0, TextSize.Y, 0)
			Label.Position = UDim2.new(0, (Frame.AbsoluteSize.X / 2) + (i * char_size/2), .5 + ((.06 * (_i-1))), 0)


			Label.Text = v:sub(i,i)
			
			Move_Chars(_i, Frame, char_size) --Moves all characters in the same line to the left
			
			task.wait(.05)
		end 
	end
end
1 Like

noooo u need 2 run it individually for each character!!!
assumign text is like a string “hello world”

1 Like

This is your issue:

local char_size = TextSize.X / #Text

Do this inside your loop instead of that:

local char = v:sub(i,i)
local charWidth = TextService:GetTextSize(char, TextFontSize, TextFont, Vector2.new(math.huge, math.huge)).X

Then use charWidth when you set the label’s size and move the X position forward

1 Like

Had that same exact idea, but here’s the thing. It just doesn’t work.


Output:
image
No matter the letter, the dimensions are the same.

Use a TextLabel with TextBounds, other than that, I don’t know why it isn’t working.

NEVERMIND??? For some reason this only seems to happen with THIS SPECIFIC FONT??? I’m going to lose my mind.


Output:
image

Threw this script together;
pretty much proves ur point

local st = "The quick brown fox jumps over the lazy dog."
local ts = game:GetService("TextService")
local font = Enum.Font.RobotoMono

for i = 1, #st  do
	local char = st:sub(i, i)
	local width = ts:GetTextSize(char, 40, font, Vector2.new(math.huge, math.huge))
	print(char .. " ; " .. width.X)
end

Huh, that’s odd. Glad you got it working! :grin: