Can you make a variable be the name of another variable?

My question is simple yet kinda confusing. I want a variable like say x, be the name of a variable say y.
So how would I do:
local [x] = “y”

process.OnClientEvent:Connect(function(map, mapNumber)
local (mapNumber) = 0,

This is what I have and it doesn’t seem to work. Keep in mind mapNumber is a number. Like an ID I plan to add to a table.

1 Like

I’m very confused as of what you are currently asking for.

My current understanding

local CoolMap1 = game.Workspace.Map

local VariableNameVariable = NameToStringFunction(CoolMap1) 
print (VariableNameVariable) -- "CoolMap1" (Is this what you are looking for??)

I don’t believe this is possible though, I could be wrong. There are a few alternatives that make this possible.

Let me know if my understanding is correct, and I’ll continue this post. If I’m wrong please re-explain what you’re trying to accomplish.

1 Like

Yes you are kind of on the right track however the name has to be the received number.

	Colors = {
	[1] = {Name = "Default", Price = 0, Color = Color3.fromRGB(0, 255, 183)},
	[2] = {Name = "Red", Price = 0, Color = Color3.fromRGB(255, 0, 4)},
	[3] = {Name = "Orange", Price = 0, Color = Color3.fromRGB(255, 115, 0)},
	[4] = {Name = "Green", Price = 0, Color = Color3.fromRGB(17, 255, 0)},
	[5] = {Name = "Blue", Price = 0, Color = Color3.fromRGB(0, 55, 255)},
	[6] = {Name = "Purple", Price = 0, Color = Color3.fromRGB(128, 0, 255)},
	[7] = {Name = "Pink", Price = 30, Color = Color3.fromRGB(255, 53, 239)},
	[8] = {Name = "LOrange", Price = 30, Color = Color3.fromRGB(255, 118, 64)},
	[9] = {Name = "LGreen", Price = 30, Color = Color3.fromRGB(74, 255, 80)},
	[10] = {Name = "LBlue", Price = 30, Color = Color3.fromRGB(99, 167, 255)},
	[11] = {Name = "LPurple", Price = 30, Color = Color3.fromRGB(140, 83, 255)},
	[12] = {Name = "Grey", Price = 40, Color = Color3.fromRGB(61, 61, 61)},
	[13] = {Name = "Black", Price = 40, Color = Color3.fromRGB(255, 255, 255)},
}

Like in this code the colors are linked to numbers.

process.OnClientEvent:Connect(function(map, mapNumber)

I would like to take the map (the actual variable/ color options) and make the name the mapNumber (The ID)

EDIT: I would like to insert it into a list such as that one.

You can’t name variables in code, but you can use a table to achieve similar functionality. It’s quite difficult to explain what you’re saying, but I think I understand what you mean.

-- OLD
local variable = 1
local variable2 = variable -- this indexes the value instead of the name of the variable which is not what you want
-- NEW
local variables = {
     ['variable'] = 1
}
local function GetFirstKeyFromValue(table, value)
   for key, val in pairs(table) do
       if val == value then
          return key
       end
   end
end

variables[GetKeyFromValue(variables, 1)] = 2

This isn’t as simple as I’m sure you hope it would be and I’m not sure if this does what you want to do within the constraints of the function.
You can’t really know which key (variable name) to use in a table unless you know the value.
A smarter system would be to just organise your values into arrays or a dictionary with preset names.

local data = {'bt5191', 'Blue'}
-- or
local data = {
   Name = 'bt5191',
   Colour = 'Blue'
}
2 Likes

My question to you is, what do you need this functionality for?
What is it that you need to be accomplished? Your original question was confusing and didn’t outline the actual problem you were facing.

Ouch, yea thats a little complex, because the code gets the ID based on what you pressed and applies it to the game to create a level using the dictionary and those ID’s are basically level numbers.

So say I have a variable that is equal to 1. Could I make a variable using that input.

local MapNumb = 1
local games = {
     [1] = {"bla bla code stuff"}
}

I need to know what the actual issue is that you are facing and the current code that you have before I can help you.

I cant set my dictionary items to their correct numbers so it breaks my entire game. I need to get the dictionary items from another script because they had 2000 lines of code each and it would lag every time I tried to type more.

Could you send the snippet of the dictionary?

image

This is the entire code for the dictionary level 103.

Okay, which part of your code handles the event?

Do you mean the receiving side of the one shown?

Yes. I mean the receiving side.

image

Wait. Couldn’t I insert a table to the current game table I have, then add everything after?

Like this.

Of course, you can index a key in the table like that. That’s how you index keys in a table with code.

local t = {}
t.hi = true
t['hello'] = 'bye'
t[3] = 2
1 Like

image

This worked. -_-
I hate my stupidity.

Thanks to bt5191

No worries, though, please don’t call yourself stupid… ¯\_(ツ)_/¯

4 Likes