Is it possible to create an indented body of text

So basically I’m making a book that players can read. Normally I would just manually create the text labels and position them to create indents. However I want to be able to load different books while doing these two things

  1. Create a new paragraph whenever there is a previously defined set of characters in the string (such as &newpara
  2. Indent each new paragrap
  3. If the paragraph extents to bounds of the text label, I want to move it to the other page.

I’ve thought about how to accomplish this and the only way I can think to do it is to manually position each character relative to the previous character. Does anyone have a better method though?

All replies are appreciated. Thanks!

Indentation (tab) is represented by \t in string.

Yes, this is possible!

Say your book is stored like this (text randomly generated for this example):

local txt = [[
&newpara
Am finished rejoiced drawings so he elegance. Set lose dear upon had two its what seen. Held she sir how know what such whom. Esteem put uneasy set piqued son depend her others. Two dear held mrs feet view her old fine. Bore can led than how has rank. Discovery any extensive has commanded direction. Short at front which blind as. Ye as procuring unwilling principle by.
&newpara
Instrument cultivated alteration any favourable expression law far nor. Both new like tore but year. An from mean on with when sing pain. Oh to as principles devonshire companions unsatiable an delightful. The ourselves suffering the sincerity. Inhabit her manners adapted age certain. Debating offended at branched striking be subjects.
]]

I can then do a gsub on that text to replace all instances of the &newpara (followed by a newline or \n) and turn it into simply a new line followed by 5 spaces. You can also use \t to “tab” a string like

print("\tHello world") --> "	Hello World"

but that doesn’t seem to render very clearly on text labels. Here’s the gsub I used for this example (instead of \t I used 5 spaces):

local bookTxt = string.gsub(txt, "&newpara\n", "\n     ")

script.Parent:WaitForChild("TextLabel").Text = bookTxt

And here’s the result:
Screenshot_11

Now that addresses your first two questions, but I don’t have too much time at the moment to explain the third other than the “approach” I would take:

I’d suggest getting the whole text from the approach above, and then using string.sub() to get substrings based on your “page”. So if each page can have 512 characters, you can string.sub(bookTxt, 1, 512) to get the first page, and then string.sub(bookTxt, 513, 1024) to get the second page and so on… Hint: there’s a formula to do that functionally. The only issue with this approach is that it would probably cut some words off, so if that bothers you you’d have to do something a bit more complex.

Hope this helps!

4 Likes

Thank you so much! This fixes almost everything! :smile:

Also if you have any good resources for string functions and manipulation like this please send me the link if you can

Sure! Honestly the docs in the devhub do a good job at explaining each of the string functions pretty decently. You can see string.gsub in there which may help explain what I did:
https://developer.roblox.com/en-us/api-reference/lua-docs/string

The next big step if you’re interested in strings is figuring out patterns, which are explained here: String Patterns

I think string manipulation is hugely important in programming, as it often is a bridge between user input, unknown data formats, and your code. I also think it’s a skill best learned by practice, so the more problems like this you work on, the better you’ll end up. Even something as simple as taking the number from a text like
"Price: $10.00"
would be immensely useful for learning strings AND practical (now you can do math on that price!). Patterns can be daunting, but starting on small problems and working your way up to big ones will help.

1 Like

You can use "\n\t" for making a new line and indenting that line instead of using \n and some spaces.