Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use a variable by using a string?

Asked by 6 years ago
local var1 = "potato"

print("var1")

I know that makes no sense because I am printing a string but what if I wanted to print the variable called var1? Is that possible and if so how would I do it?

3 answers

Log in to vote
0
Answered by 6 years ago

If you are trying to print what the variable is assigned to it's

print(var1)

But if you want the name of the variable you could put it in a table like this...

local variables = {var1 = "potato"}

And get the name of it like this...

for i,v in pairs(variables) do
    print(i) -- prints var1
end
Ad
Log in to vote
0
Answered by 6 years ago

You can print variables like this:

local var1 = "potato"
print(var1)

potato

You could also combine strings.

local var1 = "potato"
local var2 = var1 .. "E"
print(var2 .. " or " .. var1 .. "?")

potatoE or potato?

Log in to vote
0
Answered by 6 years ago

it is possible, you were almost there. Print(argument) can also be used to print variables and the outcome of functions. this can be done in the way you would normally call the variable ie. without the quotation marks:

local var1 = "potato"

print(potato)

likewise you could do the following stuff:

local a=10
local b=5

print(a-b)
-- this would be the same as
print(10-5)
-- or
print("5") -- note, the quotation marks are not neccesary here, since it is a number

quotation marks are actually used so that the software "knows": 'ohh, this is not a variable or a function that the user tries to call, but just some random text'

hope this helps!

0
this was awhile ago but i know how to script, im just wondering if I wanted to print var1 with maybe using a string. Like if I had stringValue and I wanted to print the variable that is called the stringValues value.. idk why I wanted to do this idc anymore makes no sense now. goldstorm950 65 — 6y

Answer this question