How do you change a string to a 'variable/value' so you get the argument from the function?

Like:

local function func(Health,Energy) 
local a = {"Health","Energy"}
for i,v in pairs (a) do
print(v, to?(v) ) -- I want it to print Health,1 Energy,2 and not 1,1 2,2
end
end

func(1,2)

Yeah, it’s weird how I format the code, but it needs to be done like this.

local function func(Health,Energy) 
local a = {"Health","Energy"}
for i,v in ipairs (a) do
print(v, i) -- I want it to print Health,1 Energy,2 and not 1,1 2,2
end
end

func(1,2)

Doing the loop in ipairs will be better choice. You can read something about it, my brain is too small to explain it :rofl:

Well… you got me there. But that’s not the answer I was looking for.
What if it was func(10,8)?

local values = { Health, Energy }

And then

print( v, values[ i ]

Or pass them to the function as a dictionary

function func( values )
    for i, v in pairs( values ) do
        print( i, v )
    end
end

func( {
    Health = 10,
    Energy = 8,
} )

Without any extra context on what the end goal is, it’s hard to recommend which is best for your case, or if another method not included is best. There’s many many ways to achieve this.

In something like:

local b = {}
for i,v in pairs (a) do
b[v] = tonumber(v) -- b[v] is "Health", whilst tonumber(v) is nil, and not a number I passed as a parameter.
end

I’ll try the dictionary one.

EDIT: Yup, the dictionary one works fine! Thanks!