February 2, 2023 String interpolation is the new syntax introduced to Luau that allows you to create a string literal with expressions inside of that string literal.
Lets start off simple. You want to set the health for your character whenever they lose damage, how would you do this?
local text = script.Parent.Text
text = "Health" .. HPamount
Works? Yeah of course, this is a commonly used method that isn't objectively wrong to use, but there other methods too. That's where string interpolation comes in.
local text = script.Parent.Text
text = `Health: {HPamount}`
-- > Health: 100 (or whatever amount)
Another example:
local code = {1, 2, 3}
print(`Your code is: {table.concat(code)}`)
--> Your code is: 123
print(`Your code is: {table.concat(code, ", ")}`)
--> Your code is: 1, 2, 3
To use this you need to use backticks (`) instead of quotes ("). The backtick is to the left of your (1) key.
Unlike quotes, you use the brackets to insert your variable: {variable}, instead of using "..".
print(`Some example escaping the braces \{like so}`)
--> Some example escaping the braces {like so}
print(`Backslash \ that escapes the space is not a part of the string...`)
--> Backslash that escapes the space is not a part of the string...
print(`Backslash \\ will escape the second backslash...`)
--> Backslash \ will escape the second backslash...
print(`Some text that also includes \`...`)
--> Some text that also includes `...
local name = "Luau"
print(`Welcome to {
name
}!`)
--> Welcome to Luau!