Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use a variable by using a string?

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

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

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?

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!

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.