Newline created when not needed?

Hi, I’ve been trying to create a chat system on roblox, here’s my script:

--NewLine
--Creates a new line at the end of the message

--Define variables
local textBox = script.Parent
local maxCharsConst = 91
local maxChars = 91

local chatBoxFrame = textBox.Parent
local chatBoxFrameSizeY = .055 / 2
local chatBoxFramePosY = chatBoxFrameSizeY

--Create new line event
textBox:GetPropertyChangedSignal("Text"):Connect(function()
	
	if string.len(textBox.Text) >= maxChars + 1 then --Checks when create a new line
		
		--Create new line
		chatBoxFrame.Size = chatBoxFrame.Size + UDim2.new(0,0,  chatBoxFrameSizeY, 0)
		chatBoxFrame.Position = chatBoxFrame.Position + UDim2.new(0,0,  -chatBoxFramePosY, 0)
		
		chatBoxFrame.UICorner.CornerRadius -= UDim.new(.005, 0)
		
		maxChars = maxChars + maxCharsConst
		
	end
end)

However, when this script checks how many characters the message has, it counts spaces either, and that’s a problem for this script. Here’s a video:

I know it does this because I’m checking the length of the message, but I can’t find any other method for this…

2 Likes

It could be messy, but you can attempt to write a loop that constantly checks whether there are to space characters next to each other (%s) and “merge” them (really just delete all but one). I’m terrible with string manipulation so I can’t really provide any reliable code - sorry

if you want to get rid of spaces in the string simply do

local strwithnospaces, occurences = string.gsub(str, "%s+", "")

and then you can simply take the length of strwithnospaces

local length = #strwithnospaces

hope this helps

1 Like

this could be a good method but the problem is that now, if I write a long message with spaces in it, the it won’t create a new line correctly. Here’s a video:

2 Likes

So you want to basically keep the spaces inbetween 2 not space characters right?

1 Like

My idea was to make the script make a new line every time the message “reaches the end of the textbox frame”, but since there are spaces that are smaller than normal characters, it isn’t possible because it would make a new line before reaching the end of the frame.

1 Like

Oh did you try using .TextBounds or whatever it was called here is the article. I bet you can check the y bound and then figure out new lines like that let me try to do it right now.

1 Like

actually one of the code samples might be what you wanted, here is what it would look like if editted to not account for the x axis

local textBox = script.Parent
 
-- The smallest the TextBox will go
local minWidth, minHeight = 10, 10
 
-- Set alignment so our text doesn't wobble a bit while we type
textBox.TextXAlignment = Enum.TextXAlignment.Left
textBox.TextYAlignment = Enum.TextYAlignment.Top
 
local function updateSize()
	textBox.Size = UDim2.new(
		textBox.Size.X,
		UDim.new(0, math.max(minHeight, textBox.TextBounds.Y))
	)
end
 
textBox:GetPropertyChangedSignal("TextBounds"):Connect(updateSize)

something like this

2 Likes

try this

local textBox = script.Parent

-- The smallest the TextBox will go
local minWidth, minHeight = 10, 10

-- Set alignment so our text doesn't wobble a bit while we type
textBox.TextXAlignment = Enum.TextXAlignment.Left
textBox.TextYAlignment = Enum.TextYAlignment.Top

local function updateSize()
	
	textBox.Size = UDim2.new(
		textBox.Size.X,
		UDim.new(0, textBox.TextBounds.Y + textBox.TextSize)
	)
	
end

textBox:GetPropertyChangedSignal("TextBounds"):Connect(updateSize)
1 Like

Can’t you use TextService?
TextService:GetTextSize

1 Like

Could automatic size set to Y work in this case?
image
UI Automatic Size - DevHub Link

The article shows a use case for this feature being a textbox, and it seems fairly similar to what you want to achieve:

1 Like

Good point, AutomaticSize also works

This works, but it only re-sizes it down. My idea was kind of a discord-like chat

This is what it’d look like:

I tried using TextService and TextBounds but I couldn’t find anything working for my problem…

Oh and btw, AutomaticSize doesn’t work for some reason?? It blocks creating lines after the second one

Ok, I’ve been playing around with this for a long time now and finally figured something out.

--NewLine
--Creates a new line at the end of the message

--Define variables
local TextBox = script.Parent
local maxChars = 784
local CharsConst = 784

local TextBoxFrame = TextBox.Parent
local TextBoxFrameSizeY = .050 / 2
local TextBoxFramePosY = TextBoxFrameSizeY


local TextService = game:GetService("TextService")


TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	
	local TextSize = TextService:GetTextSize(TextBox.Text, TextBox.UITextSizeConstraint.MaxTextSize, TextBox.Font, Vector2.new(TextBox.Size)).X
	
	if TextSize >= maxChars + 1 then
		
		maxChars += CharsConst
		
		TextBoxFrame.Size = TextBoxFrame.Size + UDim2.new(0,0,  TextBoxFrameSizeY, 0)
		TextBoxFrame.Position = TextBoxFrame.Position + UDim2.new(0,0,  -TextBoxFramePosY, 0)

		TextBoxFrame.UICorner.CornerRadius -= UDim.new(.005, 0)
		
	end 
	
end)

Thank you for helping me!

2 Likes