Creating a variable from a variable

So if I have the following code:

local function setVariableName(nameOfVariable, content)
     nameOfVariable = content
end

setVariableName("points", 100)

I understand that it doesn’t work that way, but how would I actually go about creating a variable from a given name. In the scenario above (if lua actually worked this way) it would create a variable named points that has the value of 100.
Is there any actual way to do this, I do not know the value before the game runs so it wouldn’t work to just create a local variable beforehand.

1 Like

To my knowledge, you can’t create dynamically named variables unless you use something like a dictionary which maps keys to values.

For example

local table = {}
table["Points"] = 100 -- Here's how you set it or you can use table.Points = 100
print(table.Points) -- here's the ways to access it
print(table["Points"])
1 Like

I was hoping I wouldn’t have to use a dictionary, but there might be no other way.
If I recall correctly there was a post on this but it took me hours to find and this was months ago, completely forgot to bookmark it.
I’m not going to mark it as an answer yet, I want to see what others have to say.

You can try using the global variable scope _G["name"] = 100 then you have access to name, but it’s not advised

For example:
p8dyaf_87591

It’s worth noting that the global variable scope is just a table anyways, it’s just accessible to the entire program.

https://www.lua.org/pil/14.html

1 Like

you can store a variable in _G, and make them dynamic.
these are global variables. this is an example:

_G.attack1 = nil
_G.attack2 = nil
_G.attack3 = nil

for _, attackAnimation in ipairs(tool.Item_Animations:GetChildren()) do
    if string.match(attackAnimation.Name, "attack") then
        if _G.attack1 == nil then _G.attack1 = plr.Character.Humanoid:LoadAnimation(attackAnimation) attacks+=1 end
        if _G.attack2 == nil then _G.attack2 = plr.Character.Humanoid:LoadAnimation(attackAnimation) attacks+=1 end
        if _G.attack3 == nil then _G.attack3 = plr.Character.Humanoid:LoadAnimation(attackAnimation) attacks+=1 end
    end
end
    
toolCombo += 1
if toolCombo > attacks then
	toolCombo = 1
end
				
if attacks > 0 then
	_G["attack"..tostring(toolCombo)]:Play()
end