Can't use \s in String

I’m using a string with leading / following spaces. Coming from using other languages, I would always use “\s” to represent this in order to ensure the code is more readable (spaces before / after sentences often go unnoticed when re-reading code). However, when trying to write a string leading with “\s” on Roblox Studio just output “s” followed by the string as opposed to creating a blank space beforehand.

Example [Roblox Studio]:

print("\sHello world!")

Output:
Output1

Expected Output [Java]:
image

Output:
image

Is it possible to replicate on Roblox Studio without using the space key?
(I know it’s a niche problem but I code in abnormal ways)

4 Likes

If you use \n it’ll add a new line:

print("Hey there!\nHello world!")

--[[ Output:
Hey there!
Hello world!
--]]

print("Example2\n\nHello world!")
--[[ Output:
Example2

Hello world!
--]]

(I think this is what you were asking about, but if I was mistaken, let me know and I can make a correction)

1 Like

I don’t want a new line, I want a space [" "], however if I type it as " " into the String it’s generally harder to notice (especially at the start / end of the string) than \s

2 Likes

Pulling from the Lua documentation, we have the following escape sequences:

Escape sequence
\a bell
\b back space
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\" double quote
\' single quote
\[ left square bracket
\] right square bracket

So, in short, we don’t have any such escape sequence. In your case, I would do:

print(" " .. "Hello world!")

to make the space very obvious.

4 Likes

Thanks, shame they included practically all the escape sequences except \s

I know this should be obvious but Lua and Java are 2 entirely different langauges and as such also have different rules and ways that strings are handled.

I would honestly just type this instead.
print(" Hello world!")

I know it’s not the prettiest but this isn’t Java after all.

I recommend checking the documentation like some other suggested.
Fortunately Lua is fairly easy to pick up.

Oh, would \t work for this use case, then? (It’s the other special character mentioned on the Strings” Roblox Creator Hub Guide that I referenced earlier.)

Interestingly, depending on the scale you have set for the Output in Roblox Studio, adding a singular \t could be the equivalent of 2 spaces or less (when it’s more zoomed in) or 4 spaces or greater (when it’s more zoomed out), so you may need to include a lot more or less depending on your preferred Output scaling.

  • For an extreme example, the last screenshot shows that using 3 \t in a row can visually appear the same as 4 spaces when using a very high scaling for the Output.

I’ll include screenshot examples after the codeblock for reference:

print("Example print for comparison")

print("\tHello world!") -- Print statement that uses \t

print("    4 spaces added at the start, for comparison")

print("\t\t\tSo many spaces!") -- Print statement that uses \t 3 times

--[[ Output:
Example print for comparison
    Hello world!
    4 spaces added at the start, for comparison
        So many spaces!
--]]

Example 1

Example 2

Example 3

Example 4

2 Likes

A tab is too big, however I appreciate the reply

1 Like

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