Scrollable textbox crash

I’m using a script to make the textbox scrollable if the text exceeds the limits, i got the code from another topic. It was working fine but suddenly it started freezing roblox studio whenever i added an extra line.

local textArea = script.Parent
local scrollFrame = script.Parent.Parent


textArea:GetPropertyChangedSignal("TextFits"):Connect(function()
	while textArea.TextFits == false do
		scrollFrame.CanvasSize = UDim2.new(0, 0, 0, scrollFrame.CanvasSize.Y.Offset + 16)
		scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.CanvasSize.Y.Offset - scrollFrame.AbsoluteWindowSize.Y)
	end
end)

textArea:GetPropertyChangedSignal("TextBounds"):Connect(function()
	if scrollFrame.CanvasSize.Y.Offset > textArea.TextBounds.Y + 16 then
		scrollFrame.CanvasSize = UDim2.new(0, 0, 0, textArea.TextBounds.Y)
		scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.CanvasSize.Y.Offset - scrollFrame.AbsoluteWindowSize.Y)
	end
end)

Script timeout: exhausted allowed execution time, on the while loop

2 Likes

just add a task.wait() or simply wait() and a number in one of the wait and put it in the loop

1 Like

I added wait() in the while loop and some goofy stuff happens now

And also

Maximum event re-entrancy depth exceeded for Instance.TextBoundsChanged

this error showed up

Goofy Stuff Happening Video

It just freezes and randomly moves after a certain amount of time

Don’t use wait, use task.wait().

They will keep firing each other making an infinite recursive loop

It worked fine before, i didn’t change anything and it just suddenly stopped working

Wait() is not optimized, and task.wait() used task scheduler which enables multi-threading.

Still the same, i don’t understand how adding a wait() would change the situation