I cant find out how to create a variable that has a name of a value of another variable

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

lets say we have a variable in a script:
variable1 = “mynextvar”
now I want to create a new variable. but the name of the new variable needs to be the value of variable1
in the real script the value of variable1 can change

  1. What is the issue? Include screenshots / videos if possible!
    i cant find out how to create a variable that has a name of a value of another variable

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    googleing how to create values (only beginners tutorials found)
    search for it on bing
    search on dev hub
    creating a variable called new and then “new.Name = mynextvar”
    creating a variable called new and then “new.Value.Name = mynextvar”
    creating a variable called new and then “newValue.Value.Name = mynextvar”
    creating a variable called new and then “newValue.Name = mynextvar”

local variable1 = "test"
local variable2 = variable1

print(variable1) --> test
print(variable2) --> test

I think OP wants to use test as an identifier.

cc @Mixu_78

But OP should explain their use case for this.

1 Like

that sets the value of variable2.
I want to set the name of “variable 2”

exacly that!!
but without the use of global variables

You can use getfenv()

var = "hello"

getfenv()[var] = "hi"

print(hello)

This basically returns the “environment” of the function passed in the argument of getfenv() so that you can do things like getfenv(functionName) to return the environment of the function.

This is similar to a table of variables allowing you to set various things within the function and global environment of a script via indexing.

function printMessage()
   print(message)
end

local env = getfenv(printMessage)
env.message = "Hello, function environments"
printMessage() --> Hello, function environments

Example ripped from the devwiki.

2 Likes

I was typing what i was doing, and it was getting quite long.
in that time someone already posted the solution
thanks to everyone trying to help :smiley:

1 Like

No, don’t do that. If this is for a module, editing the environment of the requiring script completely defeats the purpose of modules. Also using global variables is bad practice; it makes your code messy.

cc @haash_im

1 Like

he did succeed when trying to figure out what I wanted to do.
what would you suggest?

Reconsider your design. Do you really need this? Your explanation (I read it before you deleted it) didn’t really make much sense, so if you could re-explain it that would be appreciated

1 Like

basically it is a collaboration of 3 different scripts
controller-universe-model (each of these is a script in some way)
the universe holds signals
the controller can edit signals that exist in the universe
the model can read signals from the universe and perform functions related to those signals.
(why I chose to do this is a long… long story)

at the moment I am working on the model trying to make it listen to the right signals from the universe.
for that, you need to know what is inside the universe.
the universe has signals ranging from C001 to C100 every signal can be assigned a number value
every model has a starter address (say C040) and a set number of signals to listen for (let’s say its 6 for this one so that means C040, C041…C046)

local Channel1 = game.workspace.universe.C040
Channel1.Onchanged:Connect(Function(C040)
print("whatever needs to happen")
end

local Channel2 = game.workspace.universe.C041
Channel1.Onchanged:Connect(Function(C041)
print("whatever needs to happen")
end

the issue is that the address and the amount of channels to listen to is unknown and can vary between models

I hope this helps and clear things up.
don’t mind the actual code written inside of this. as it is just an example

That won’t work. _G behaves differently in vanilla Lua from in Luau. _G is just a table that all scripts can access, not the environment table.

2 Likes