Issues with TextSizing

Hey Developers!

I am currently creating a nametag script that loops through a player’s username and creates a separate label for each letter. I do this so that I can tween each letter accordingly to a player’s equipped TextEffect.

Here’s my code so far:

function PlayerAdded(Player)
	local NametagStyle = "Wave"
	
	local function CharacterAdded(Character)
		local Nametag = script.Nametag:Clone()
		Nametag.Size = UDim2.fromScale(#Player.Name/2.5, 1.5)
		
		local Labels = {}
		
		local Count = 1
		for Letter in string.gmatch(Player.Name, "(%w)") do
			local TextLabel = Instance.new("TextLabel", Nametag.Letters)
			TextLabel.Size = UDim2.new(.05, 0, .75, 0)
			table.insert(Labels, Count, TextLabel)
			TextLabel.AnchorPoint = Vector2.new(.5,0)
			TextLabel.Text = Letter
			TextLabel.Name = Letters[Count]
			TextLabel.BackgroundTransparency = 1
			TextLabel.Font = Enum.Font.GothamBold
			TextLabel.TextColor3 = Color3.new(1, 1, 1)
			TextLabel.TextScaled = true
			Count += 1
		end
		
		Nametag.Parent = Character.PrimaryPart
		
		local function Center()
			for I, Label in pairs(Labels) do
				
				Label.Size = UDim2.fromScale(1/#Labels, 1)
				Label.Position = UDim2.fromScale(I/#Labels - (Label.Size.X.Scale/2), 0)
			end
		end
		
		Center()
		
		while Character do
			wait(5)
			NametagFunctions[NametagStyle](Labels)
		end
	end
	
	CharacterAdded(Player.Character or Player.CharacterAdded:Wait())
	
	Player.CharacterAdded:Connect(CharacterAdded)
end

Now, this randomly works perfectly for my username:
image

But for other usernames like these, this happens:


image

Switching from TextScaled to TextSize leads to issues like this:
image

Does anyone have a fix for this? Thanks!

As it so happens I’m working on something similar right now. The problem here is that not all letters take the same amount of space. Text scaled is telling the smaller characters that they need to be bigger. Text size is definitely the way to go. But as your image shows, they are all now spaced equally apart, and by quite a bit. This is just because you are assuming constant size and probably don’t have a reference for how large the whole text should be. I haven’t finished yet as I’m still setting up some other things, but the way I’m planning to do this right now is to use this property TextLabel | Roblox Creator Documentation and loop through all of the labels keeping track of how far it needs to push the next letter. Then because my use case requires it to be centered I’ll take the size of that and compare it to the absolute size of the container frame, then loop back through and center it with the offset.

1 Like

Having a bit of trouble getting this to work on my own. Given the code provided, how would you integrate using TextBounds as I am using scale and not offset.

Update, here’s what I have that is working for now. Text spacing is a little off, any suggestions with that? Specifically around centering.

local function Center()
			for I, Label in pairs(Labels) do
				
				Label.Size = UDim2.fromScale(1/#Labels, 1)
				Label.Position = UDim2.fromScale(I/#Labels - (Label.Size.X.Scale/2), 0)
				Label.Size = UDim2.new(((1/#Labels)/10)*Label.TextBounds.X,0,1,0)
				Label.TextScaled = true
			end
		end
		
		Center()

You need a way to determine the distance of each character from the center of the total size of the string. The way I’m doing this is by actually having a table I can go through that holds all of the positions of each character starting at 0, Then I subtract half of the fullStringSize from each of those giving me the position relative to the center of the fullStringSize. (I would recommend still using offset and converting offset to scale at the end). Then you can just add the relative position of the character to the center of the parent.

Something like this

for index, char in characters:
  DistanceFromCenterOfFullString = characterPositions[index] - FullStringSize --Keep in mind you have to add all of the text bounds together to get the fullStringSize
  char.Position = UDim2.fromOffset(char.Parent.AbsoluteSize.X/2 + DistanceFromCenterOfFullString, 0)
  char.Position = UDim2.fromScale(char.Position.X / char.Parent.AbsoluteSize, 0) --Converts offset to scale.