Hello Developers
This thread is a re-thread because I’m not sure if i should edit the other thread and make new one. Here’s the Original Thread. Well continuing on the topic. i got this problem where you add space depending on the name(like normal roblox chat). i fixed this last time by doing (Name len)+(Text Size) like the original thread i posted. so i assumed its now working so i continue doing other script needed for my game. Then i came to this, Multi-player test. i did the 2 player test then this happen to the chat. There’s an extended spaces on it. if there could be other script for this, please help me with the script.
You see, the thing with proportional fonts is that they have variable width. Most characters are wider than a space, but how much wider depends on the character.
What you need to do, and what the default Roblox ChatScripts do, is first:
Measure the width of a space. I’d do this by putting, say, 20 spaces in a label, then dividing its TextBounds.X by 20
Measure the width of whatever you’re trying to fill with spaces, then calculate how many spaces you need by math.ceil(desiredWidth / spaceWidth). Note the default ChatScripts use ceil but you may want to use round instead.
Or just use RichText (which wasn’t out at the time when the ChatScripts were first introduced). Here is the fastest way to escape a string for RichText, confirmed by benchmarks:
local escapes = {['&'] = '&', ['<'] = '<', ['>'] = '>', ['"'] = '"', ['\''] = '''}
local function __escape_gsub(match: string): string
return escapes[match]
end
local function xmlEscape(text: string): string
return string.gsub(text, '[&<>"\']', __escape_gsub)
end
i didn’t use rich text because users can just use “<b_>some text here</b_>” or change the rich text color making it destroy the game as there will be different color for each. like
Guessed the answer = green
User already answered = green
Kicked/ Banned = orange. That’s why i don’t use rich text
That’s why I included xmlEscape - it makes <b> show up as the literal text <b> instead of actually making things bold. That’s because it replaces the brackets with < and >, which are special and show up as < and > without actually counting as a tag.
xmlEscape makes it so that no syntax at all works. To be perfectly clear, “escaping” converts a string into a different string that looks the same after being parsed by RichText. So for example, if someone sent <b>hi</b>, xmlEscape would convert it to <b>hi</b>, which would then show up as <b>hi</b> in the GUI, without actually making anything bold.
Outside of xmlEscape, you can still use whatever syntax you want. So as long as you use '<b>' instead of xmlEscape('<b>'), it will show up as bold. Just make sure to put the username & chat message inside of xmlEscape.
'<b>' .. xmlEscape('<i>hi</i>') .. '</b>' would show up as <i>hi</i> in the GUI. Your <b> tag went through, but since the <i> was in the xmlEscape it didn’t apply. It’ll be the same if you use <font> instead of <b> - it’ll work outside the xmlEscape.
You can use TextService’s “:GetTextSize()” instance method to find the size of an area required to contain a single whitespace character of some font size & style. The same can be done for the player’s username with the same font size & style, then all you need to do is divide the latter by the former and math.ceil() the result in order to determine how many whole whitespace characters would be required to cover the length of the player’s username.