Help with dialog system detecting out of bounds text

I am making a dialog typewriter system, and a feature I wanted to add was that it automatically calculated whether or not if the text was out of bounds. If so, it would wait for the mouse’s input, clear all the text, and write the remaining text down.
However, I can’t seem to correctly calculate whether or not the text is out of bounds.
Here is a video:

Notice how the rest of the A’s go out of the TextLabel? If my system worked correctly, it would have detected it went out of the TextLabel beforehand, and waited for user input.

Here is my code:

return function(toad)
	local Players = game:GetService("Players")
	local TextService = game:GetService("TextService")
	
	local Dialog = toad.Class("Dialog")
	local Maid = toad.Maid

	function Dialog:__init()
		local _Maid = Maid.new()
		
		self.MouseEvent = game:GetService("Players").LocalPlayer:GetMouse().Button1Down
		
		self.GUI = script:FindFirstChild("Dialog"):Clone()
		self.GUI.Parent = toad.PlayerGui
		
		local Shadow = toad.RoStrap:LoadLibrary("PseudoInstance").new("Shadow")
		Shadow.Elevation = 8
		Shadow.Parent = self.GUI.Back

		local label:TextLabel = toad.PlayerGui:WaitForChild("Dialog").Back.Text
		self.label = label
		local Params = Instance.new("GetTextBoundsParams")
		self.Params = Params
		self.Params.Text = label.Text
		self.Params.Font = label.FontFace
		self.Params.Size = label.TextSize
		self.Params.Width = label.AbsoluteSize.X
		
		function self.VerifyBounds(label, Text)
			self.Params.Text = Text
			self.Params.Font = label.FontFace
			self.Params.Size = label.TextSize
			self.Params.Width = label.AbsoluteSize
			
			return TextService:GetTextBoundsAsync(self.Params)
		end
		
		self._Maid = _Maid

		_Maid:GiveTask(
			label:GetPropertyChangedSignal("MaxVisibleGraphemes"):Connect(function()
				self.Params.Text = label.Text
			end)
		)
		
		label.MaxVisibleGraphemes = 0
		label.Text = ""
		
	end
	
	function Dialog:say(Text)
		local GUI = self.GUI
		local _Maid = self._Maid
		local TextLabel : TextLabel = self.label
		
		local function removeTags(str)
			str = str:gsub("<br%s*/>", "\n")
			return (str:gsub("<[^<>]->", ""))
		end
		
		local Text = removeTags(Text)
		
		TextLabel.Text = Text
		
		for i, v in utf8.graphemes(Text) do 
			local grapheme = Text:sub(i, v) 
			local falseLapse = Text:sub(1, i + 1)
			print(falseLapse)
			local remaining = Text:gsub(Text:sub(1, i), "")
			
			local lapse = Text:sub(1, i)
			local bounds = self.VerifyBounds(TextLabel, falseLapse)
			
			
			TextLabel.MaxVisibleGraphemes = i
			
			if bounds.Y > TextLabel.AbsoluteSize.Y then
				self.MouseEvent:Wait()
				TextLabel.Text = ""
				Dialog.say(self, remaining)
			else
				wait(.05)
			end
		end
	end
	
	return Dialog
end

TextLabels have a read-only TextFits property which changes to false if the text goes out of bounds. You can use GetPropertyChangedSignal to detect when that property changes to false and then stop you code from running

Thanks! That worked.
For anyone else wondering, I cloned my actual TextLabel and turned it into a dummy TextLabel, where I could write out the text and check if TextFits was false.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.