How to seperate text over lines based on spaces?

I want to separate text onto multiple lines based on if it’s got a space. I know /n separates a text in a TextLable to multiple lines For example

local Text1 = "Hello"
local Text2 = "Hello World"
local Text3 = "Hello World Again"

-- Text1 on 1 line
-- Text2 on 2 lines
-- Text3 on 3 lines
1 Like

Look into using RichText. This will allow you a lot more flexibility in formatting text: Rich Text Markup | Documentation - Roblox Creator Hub

I’m a bit confused, do you want to check if it has a space and then add /n?

It’s \n (backslash n), and, if I’m understanding him correctly, he wants to space each word into a separate line which would require RichText. Using a \n doesn’t separate text into separate lines on GUI text objects.

EDIT: You can scriptably space lines using \n (see below for more)

1 Like

You could just swap the spaces with “\n” using gsub.

myString = myString:gsub(" ", "\n")

1 Like

Actually it does make text turn into multiple lines without richtext.image

1 Like

Nevermind, I redact that statement. You can space text using \n, but through script.

game.StarterGui.ScreenGui.TextLabel.Text = "Hello".."\n".."World"

This spaced the text for me when run through the command line and fixed the above issue!

image

3 Likes

An easy way to do this would look something like this:

local text = "Hello World Again"
local separatedText = table.concat(string.split(text, " "), "\n")
print(separatedText)

Then you could set the .Text property of a TextElement (TextLabel, TextButton) equal to seperatedText like this:

textLabel.Text = separatedText

Another solution would be to use string patterns like @korky5000 suggested.

I guess you could also use a “UIListLayout” insid of a frame, if you put your different text objects in there (If Text1-2 and 3 are seperate texts) it would automatically line them up in a list/paragraph way.
(This can also be combined with a ScrollingFrame)

If this is not what you want to achieve then forget whatever I have said.