How to Reference a Table by a String

If I have something like:

local BadGuy = require (Classes.BadGuy)

and then I have a text variable:

local TextVar = “BadGuy”

then how can I reference the BadGuy table by using the text variable?

For example, everything is fine if I call

BadGuy:new()

but it fails if I call

TextVar:new()

So how can make it so that my text variable, which is the same name as the BadGuy table variable, can be used to initiate the BadGuy class? Is there a way to change the type of a variable from text to table?

The reason I ask is because I’m reading in information from a JSON file and trying to dynamically create class instances.

The safest way is using another table.

local readFrom = {} readFrom[TextVar] = BadGuy -- Now readFrom.BadGuy exists.

Works perfectly! Thank you.

The safest way is using another table.

local readFrom = {} readFrom[TextVar] = BadGuy -- Now readFrom.BadGuy exists.

Works perfectly! Thank you.