Accessing a variable from a string

I was thinking about how javascript can access a variable within a string by doing

const var = 5
console.log(`String ${var} more String`) // "String 5 more String"

does lua have any similiar capability? Curious.

I’m not quite sure what you mean by that but you can use

modifiers to remove all the letters then all you have is a string full of numbers that can be converted to a number value since lua is a loose based coding language if i’m correct

though thats not good practice to get into and only works with those types of languages

local var = 5
print(string.format("String %s more String",var)) -- String 5 more String

You mean something like this?

1 Like

I think I know what you mean. Is this what you’re looking for?

local var = 5
print("String "..var.." more String") -- "String 5 more String"
1 Like

it’s likely string interpolation will be coming to Luau in the future as there was a commit made awhile ago by KampfKarren to support *% specifier in anticipation for string interpolation which would truly implement what you mentioned.

string.format now supports %* in preparation for string interpolation support.

https://developer.roblox.com/en-us/resources/release-note/Release-Notes-for-539

In the meantime, you can use concatenation or string formatting to do it as other replies have mentioned.

local str = 'some string'

print('String ' .. str .. ' more String')
print(string.format('String %s more String', str)

Another thing to note is that Roblox’s print function allows for any type, including tables which IIRC using ${var} supports as well, so something like this would also work:

local t = {}
print('t is equal to', t)

If you want to get into the more advanced stuff, you can also use metatables using the __concat metamethod.

local t = {}

function t:__concat(value)
    return self.Name .. value
end

local myObj = {}
myObj.Name = 'super cool table'

setmetatable(myObj, t) print(myObj .. ' is cool') -- super cool table is cool

The print function also invokes the __tostring metamethod so if you want to return a property of the table like a name or class name, you can do that as well to make your print prettier.

local t = {}
function t:__tostring()
    return self.Name
end

local myObj = {}
myObj.Name = 'super cool table 2'
setmetatable(myObj, t)

print(myObj, 'is cool') --> super cool table 2 is cool

Yeah, I’m pretty sure this is the answer to @PerfectlySquared’s question. I don’t know why everyone is over complicating it.

Not over complicating it. Showing them more ways to do it so if they want to do it a different way.

1 Like

Doing metatables for concatenation is definitely overcomplicating it.

It’s called learning. I use a module I made specifically for concatenation, so I can do entire table in 1 string if i have too, etc.

string.format is actually the preferred method for this, though the other way works it has a lot of limitations and if you ever go to another language it will come back to bite you if you don’t spend a few seconds reading about string.format.

The one person who did that isn’t everyone and it was only an option of many.

Well, yeah. I was more talking about this:

local t = {}
function t:__tostring()
    return self.Name
end

local myObj = {}
myObj.Name = 'super cool table 2'
setmetatable(myObj, t)

print(myObj, 'is cool') 
2 Likes