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…
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
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:
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.
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.
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)
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)
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)