Question About combining a variable w/code

Hey, so I was wondering if there is a way to combine a line of code trying to get a value of something and a stringvalue to find the name of the value. This might not make sense, but something like this:

script.Parent.Activated:Connect(function()
    local Test = "s"
    print(game.Players.LocalPlayer.leaderstats.Gem['Test'].Value)
end)

So I want this to print the value of “Gems” not “Gem” if that makes sense. Let me know if you have any ideas on how this could be possible. Might just be something that im just not thinking about, but im probably staring at the answer.

print(game.Players.LocalPlayer.leaderstats["Gem"..Test].Value)

Yup, I was knew I was like staring at it pretty much

What would this look like if it were a table? Kinda like:

script.Parent.Activated:Connect(function()
    local Test = "1"
    local Test1 = {"yes"}
end)

and say im trying to print the first child of test1 what would this look like?

If you’re trying to print a string within the table, it would look like this:

print(Test1[1]..Test)

Use tostring if the item inside the array (table) is not a string:

print(tostring(Test1[1])..Test)

And, if you are just printing, you can type this:

print(Test1[1], Test)

What do you mean? You mean you want to index the nth value of Test1 where “n” is described by Test?

If Test is a string, you will need to convert it into a number first like this:

local index = tonumber(Test)

Then, you can simply index it directly

local index = tonumber(Test)
local value = Test1[index]
print(value)

Condensed version:

print (Test1[ tonumber( Test ) ] )
-- You don't need the spaces, I just put them for clarity

Sorry lol, I mean like getting the table and things so:

script.Parent.Activated:Connect(function()
    local Number = "1"
    local Test1 = {"yes"}
    print(Test[Number][1]) -- or something like this
end)

so pretty much getting the name of the table besides the number, then getting the number and printing the first value (idk if this makes any sense lol)

So you want to get the variable called “Test1” from “Test” and “1”?

Like this but with a table if that makes sense. So get the table name without the number, input a number, then get the first input of the table

You cannot get a variable from a string. The table would have to be inside of another table or dictionary.

Ex. 1:

local arrays = {Test1 = {"Yes"}}
local arrayName = "Test1"
print(arrays[arrayName][1])

Ex. 2:

local arrays = {{"Yes"}}
local arrayIndex = 1
print(arrays[arrayIndex][1])

Ex. 3:

local arrays = {Test1 = {"Yes"}, Test2 = {"No"}}
local arrayNumber = 1
print(arrays["Test"..tostring(arrayNumber)][1])

Like this: How to Get a Variable From a String

1 Like