How can i add spaces by len depending on username?

Hello Developers,
I would wanna know, how do I add spaces by username? like the Roblox core chat?

I saw a thread like this here: How would I calculate the amount of spaces needed depended on a username?
but I have no clue or I don’t get how to do it with custom chat.


I copied:

function methods:GetNumberOfSpaces(str, font, textSize)
	local strSize = self:GetStringTextBounds(str, font, textSize)
	local singleSpaceSize = self:GetStringTextBounds(" ", font, textSize)
	return math.ceil(strSize.X / singleSpaceSize.X)
end

What i made:

function methods:GetNumberOfSpaces(str, font, textSize)
	local strSize = self:GetStringTextBounds(str, font, textSize)
	local singleSpaceSize = self:GetStringTextBounds(" ", font, textSize)
	return math.ceil(strSize.X / singleSpaceSize.X)
end
game.ReplicatedStorage["ChatStorage(-)"]['Normal(-)'].OnServerEvent:Connect(function(player,chat)
	methods:GetNumberOfSpaces(chat, Enum.Font.SourceSansBold, 32)
end)

What I got:

 ServerScriptService.Script:20: attempt to call a nil value 

Thank you so much for helping me, i really appreciate it!

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local space = " "
	local spaces = space:rep(player.Name:len())
	print(spaces.."\n"..spaces:len())
end)

Are you trying to replace the player’s username with a number of spaces matching their username length?

1 Like

Yes, and I’ll try the code tomorrow since im currently on phone. Thanks!

Well, there’s another problem.
image
i tried using output since i didn’t make the chat yet.
but it makes several spaces.
image

image

Like that.

Nvm, i fixed it.
image

Another problem,
the spaces isn’t really working, as you can see, my chat message is on the middle of the username.
image

Make sure the font-size and font-style of the name label and the message label are the same, I’m assuming the size of the label matters too.

Same fonts:
image
image

Array:
image

Text Size:
image

Size:
image

Nvm, i finally fixed it by doing
(Name len)+(Text Size)+9(For :). Thanks for the script btw.

Results:

image