Concatenating variables into strings

current code (which doesnt work)

I want the code to be something like this, it has to be surrounded in the double square brackets.

edit: after a while of busing my brain, i figured out I had simply just forgotten to implement the ending operator after concatenating the variable

You can use string formatting to accomplish this.

By the way, the problem in your syntax is that you’re missing the concatenation operator after the answer insertion, but string formatting is cleaner anyway.

4 Likes

What you’re wanting to do is called “string interpolation.”

No. Lua doesn’t have that built in. The closest you can easily get is string.format, like what @Avigant suggests. There are examples of making your own string interpolation functions online, however.

Your code is not working because you forgot to write the concatenation .. symbols between some sections of your string currently formatted like this:

answers[1] [[

They should be instead formatted like this:

answers[1] .. [[

Don’t do that to answers[8], however.

1 Like