How can I name variables with other variables?

Let me break down what I am trying to say:

local var = 5
local Step[var] = game.Step[var]:Clone()

In this example, I would like the second line to result in the following variable being created in the code:

local Step5 = game.Step5:Clone()

I tried something like this in some test code, and it did not work. Is there a way to name a variable with other variables included?

You’d want to use a table for this:

local Steps = {} -- The table to hold the values
Steps[var] = game["Step" .. var]:Clone() -- Concatenate the "Step" prefix and the current value 'var' together
-- Add the var to the table

Although I don’t recommend this, you can use getfenv:

local var = 5
getfenv()["Step" .. var] = game:FindFirstChild("Step" .. var):Clone()

You should rather just use tables as @HugeCoolboy2007 suggested.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.