Issues with centering text

Hey developers! I am trying to make a nametag system similar to that of @Merely in Trade Hangout. You can play what I have so far here:

The main issue I am having is centring the text. Here’s my issue in bullet points:

  • Every letter must be it’s own label or I can’t tween each letter induvidually.
  • I can’t use a UIListLayout because then I cant tween the position of the labels.
  • I can’t have every label the same size and the same distance apart or the nametags will be wonky.

Here’s my current script for this:

local function RefreshNametag(Character)
		if Character.PrimaryPart:FindFirstChild("Nametag") then Character.PrimaryPart:FindFirstChild("Nametag"):Destroy() end
		local Nametag = script.Nametag:Clone()
		Nametag.Size = UDim2.fromScale(#Player.DisplayName/2.5, 1.5)

		local Labels = {}

		local Count = 1

		for i = 1, #Player.DisplayName do
			local Letter = string.sub(Player.DisplayName, i, i)
			local TextLabel ="TextLabel", Nametag.Letters)
			table.insert(Labels, Count, TextLabel)
			TextLabel.AnchorPoint =,0)
			TextLabel.Text = Letter
			TextLabel.Name = Letters[Count]
			TextLabel.BackgroundTransparency = 1
			TextLabel.Font = Enum.Font.GothamBold
			TextLabel.TextColor3 = ConvertColor3(Color)
			TextLabel.TextSize = 30
			Count += 1

		Nametag.Subtext.TextLabel.Text = "@" .. Player.Name

		Nametag.Parent = Character.PrimaryPart

		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 =*Label.TextBounds.X,0,1,0)
			Label.TextScaled = true

		delay(1, function()

			while Character do

Now, this works perfectly fine for usernames such as mine:

But for certain other usernames, the spacing is very off:

Does anyone have a fix for this? Thanks!

You weren’t taking into account that the letters are a different size when centering. I mean you are a little, but not enough.

You need a way to determine the distance of each character from the center of the total size of the string when all textLabel sizes are added together. I would have 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. (I can’t get this to read well… hopefully the psuedocode below will help you grasp what I mean)

FullStringSize is made by adding Label.TextBounds.X from all labels together to get the full actual size of the string.

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.

If something seems off about my response it’s probably because most of it is a copy from Issues with TextSizing - #4 by oskxzr

1 Like

You can try using list layouts or by looping through it text label and scaling to size. I would also not use text scaled since that can screw up the letter size so just setting them to like 40 should do.

You could technically use a UIListLayout if you put the TextLabel inside of another frame. That way you can tween the TextLabel itself, while the frame’s position is in the UIListLayout.

Where before you might be doing this:

You would do this instead:

@tlr22’s method is probably better, but a little more complicated. Either solution should work though.

1 Like


In the script I’m setting the size of the text label to it’s TextBounds with some arithmetic performed on it and then setting it to textscaled, so I don’t have the issue of different sizes.

Oops! Forgot I made that. Thanks for your solution.

Why didn’t I think of that- I’ve literally done it before.

1 Like