How do I correctly paragraph chunks of text?

I’m working on a dialogue system that uses custom text (for the aesthetic) and I’m not sure on how to stop at certain points where the word goes beyond the frame.

image
What I want to achieve is that after “afas” it goes into another paragraph which reads “Hello yes”

local sizemod = 0
	local paragraph = makeparagraph()
	paragraph.Parent = Gui
	paragraph.LayoutOrder = index
	
	for character in txt:gmatch(".") do
		local element = makeframe(character)
		local label = element.TextLabel
		label.TextTransparency = 1
		label.TextStrokeTransparency = 1
		label.Position = UDim2.new(0.5, 0, 0.5, 2)
		
		sizemod += element.Size.X.Offset
		
		if sizemod >= 397 then
			paragraph = makeparagraph()
			paragraph.Parent = Gui
			paragraph.LayoutOrder = index
			sizemod = 0
		end
		
		
		-- Animate:
		element.Parent = paragraph
		index += 1
		TweenService:Create(label, TweenInfo.new(0.1), {TextTransparency = 0, TextStrokeTransparency = 0.5, Position = UDim2.fromScale(0.5, 0.5)}):Play()
		
		-- Delay:
		local extension = character == "," and 0.35 or (character == "." or character == "?" or character == "!") and 0.75 or 0
		task.wait(0.05 + extension)
	end

Hope this helps

I’m gmatching through each character and creating it’s own frame. This is not a textlabel on it’s own, and rather just UIListLayouts doing their work.

image

But each frame that contains text label you can add a script mentioning textwrap=enabled

Just a idea
loop through the text aka do this

for i=1,#urstring do
	local v = string.sub(urstring,i,i)
	if v == string.upper(v) then
		-- make new paragraph bla bla bla
	end
end