Newline not working with stringvalue

I have a StringValue with a newline in it, but when I set a TextLabel’s Text to it, it doesn’t register, and instead just puts it as part of the text, any ideas?

Example:

StringValue.Value = "Test \nTest"
TextLabel.Text = StringValue.Value

TextLabel then displays “Test \nTest” instead of;
“Test
Test”

Can’t say I’m getting the same thing. I ran the following code in the command bar. It will display the two words in separate lines.

local sv = Instance.new("StringValue")
sv.Value = "Test \nTest"
local tl = Instance.new("TextLabel")
tl.Text = sv.Value
local cg = game:GetService("CoreGui")
local sg = Instance.new("ScreenGui")
sg.Parent = cg
tl.Parent = sg
tl.Size = UDim2.new(1, 0, 1, 0)
tl.TextSize = 32
wait(2)
sg:Destroy()

We cannot help you debug an issue with your code if you do not provide us with the code you’re running. The example that you have provided will error on the first line due to invalid syntax.

If I am correct, I believe you’re setting the Value property using the property editor widget and not from with Lua. If this is the case then the Value property will actually be

Test \\nTest

When setting the Text property of your TextLabel, you will see

Test \nTest

This all occurs due to character escaping. When you write \n in the property widget, the \ needs to be escaped so that it is displayed correctly by UI elements. Hence why you end up with \\n for the actual string.

2 Likes

Figured out you can repro it by manually typing test \ntest into the value property. Manually assigning the value within the properties window doesn’t produce the same result as assigning it via the command bar or a script instance.

image
vs

image

edit: accidentally replied to response instead of op

Ended up just copying the newline character from shift+enter, and put it in the StringValue. Probably not the best solution, but it works

Alternative Suggestion


Try using block strings instead:

local blocc = [[
    Hello!
    This is a block string!
    :D
]]
2 Likes

Just saying that the whitespace in that string will be counted. Starting a line break at the beginning of the quote will show up. Your code will have the tabs showing up as well.

You can write your block strings without whitespace or construct them using concatenation.

local stringBlock = [[Hello.
This is a block string.
Bye.
]]

local stringConcat = "Hello."
    .. "\nThis is a concatenated string."
    .. "\nBye."
3 Likes