I’ve got a typewrite script that writes out a given text on different text boxes. My issue is that when the text goes from one TextBox to another, the words are cut off. Is there a way to keep the words together when using string.sub?
I’m currently unable to provide a screenshot, so here would be an example:
This is an example of text t
hat may or may not be cu
t off at any moment.
You should check out the string documentation to see if there are any functions there that could help you. I can’t see the script because as you said you can’t provide a screenshot, but if I had to assume you’re using string.sub to manually set the length of the substring which could be problematic in this case.
You could use string.split() to split the words in one string into a table, and then use a loop with string.len() to add words to a string while also keeping track of the character length, starting in a new text box if the length goes over the amount that we need. I’m unsure how advanced your scripting knowledge is (as you haven’t posted a screenshot) but I’d take this approach:
local text = "This is an example of text that may or may not be cut off at any moment."
local wordTable = string.split(text," ") -- we'll put every word in a table
local newText = ""
local maxCharacters = 28 -- replace this with however long you want your strings to be.
for i,v in pairs(wordTable) do -- loop through every entry in the table
if string.len(newText) < maxCharacters then -- if the length of our newText string is smaller then our max characters
newText = newText..v.." " -- add the new word (v) to our newText string and add a space at the end
else -- if not
newText = string.sub(newText,1,string.len(newText)-1) -- we'll get rid of the space at the end of the string
print(newText) -- replace this with however you put your text into the correct elements.
newText = v.." " -- we'll add the current word in the loop to the start off the new string.
end
end
if newText ~= nil then -- we'll make sure to print the last string after the loop has finished, as long as there's something left
newText = string.sub(newText,1,string.len(newText)-1) -- getting rid of the space at the end again
print(newText) -- replace this with however you put your text into the correct elements.
end
I’m no expert but this seems to work considering your use case. the script above prints:
This is an example of text that
may or may not be cut off at
any moment.
Of course, follow MP3Face’s reply if you are only using one TextBox element to do this whole thing, if not, you can use this.