Way to rename variable to another variables name?

so what im trying to do with my script is very crucial that it is able to do this and I cant find a way because when I do it, it has an error:

essentially what I want to do is this:

local Tab = {
["String"] = "Hi"

local Tab["String"] = 54

Any thoughts?

To modify a dictionary item, remove the “local” part and it will work.

No, im not trying to modify the dictionary item. I’m trying to set the name or index of a variable to that dictionary items value, so essentially it should be the equivalent of saying:

local Hi = 54

As far as I know, that may not be possible.

The closest answer that I can currently think of is

local OldVariable = 6+9
local NewVariable = OldVariable

I found a way to do it, but it’s not very efficient I guess because you have to separate the variable from a table to get it on its own:

so it basically works but the variable is inside of another table, which I have to find out how to remove it

Do you mean how to get a key in the dictionary or how to rename keys? I am confused.

Is there a reason why you can’t use a dictionary? I tested it and if you make a second dictionary with the key being the value “Hi” from the first dictionary, you can use it like you would a variable just with the dictionary.

yes, because im making a system where the variable needs to have a specific name given to it by a client and it can only be a variable not a dictionary, as the name of the variable that contains a dictionary is important because it is addressed later on in the script.

So your saying the player inputs the variable name? It would be the same just with it as a key accessing the dictionary value.

dictionaryName[“Player Input Here”]

ok so how you would give a variable a name like:

local cat = 5

I am saying that I want to use a predefined name to give that variable a name, like so:

local Kitten = "String"
local Kitten = 4

print(Kitten) --> 4, Variable name kitten.

However that isn’t how variables work and is confusing so what I used the dictionary for was to say:

local Table = {
["Kitten"] = "Name"

local Table["Kitten"] = "Kyle"

so what I did here was take the value of Kitten in the table and set the name of the variable below to its value aka Name, and gave it a different value

so if this were to work, when I print:

print(Name) --> Kyle

However roblox wont allow me to do this so I am looking for an alternative or some way to do it.

hopefully that was able to help you understand what I’m asking.

You will have to get things straight out of the dictionary and print them then. As that is faster and possible.

Also you are printing a table in your original script. Tables can’t be printed

Tables can be printed.

and there is a reason that it needs to be renaming a variable to another variables value

There’s no way to do what you want. From what I’ve understood your best option would be to create a second dictionary as I described above.

alright then. Thanks anyways, thank you for your time.

If you want to rename the key in a dictionary, you’re going to have to assign the old key to nil and then create a new key by the name you want it to be and set the value.

dict["Key1"] = "hello"
dict["Key2"] = dict["Key1"]
dict["Key1"] = nil

If this doesn’t work, it’s possible that it may be using a reference (i.e. local t = table; local i = t; t = nil; i == nil -> true so you can probably do dict["Key1"].."" and it should make a new reference, though I may be wrong on that.

1 Like

no, i’m not sure how to explain this. I want to rename a variable to another variables name.

if I have a variable called:

local cat = 5

I want to change it so that the name of the variable (cat) is something like dog. so it still has the same value I just changed the name. this is crucial especially if the variable is holding a dictionary.

Again, you have to set a new variable to the value of the old one and remove the old one. There is no way to rename a variable without changing the memory reference.

okay, thanks. i’ll have to use another method then