Hi!
I'm attempting to change a value in a table. My code is as follows:
local data = { Apple = false, Inside = { Value = 123 } } -- valueChanging is something such as "Apple" or "Value" inside of "Inside" -- newValue is what the value will become. local changeData = function(valueChanging, newValue) data[valueChanging] = newValue end changeData("Inside.Value", 424) -- DOES NOT WORK changeData("Apple", true) -- DOES WORK
I definitely know that the problem is something to do with the 2nd argument with the changeData function but I'm not sure how to solve it! I am really concerned about the "Table.Value" being the cause of the problem.
The worst thing is is that when you run this code, there are NO ERRORS. I am super confused.
Any help?
Thanks! Nathan.
The first problem is that you can't index things in a string. If you want to reference a value, you don't put it inside a string, you just put it in on its own.
value = "Hi!" funcName("value") -- sends 'value' funcName(value) -- sends 'Hi!'
Unfortunately, even if you fixed this problem, it would still not work. The way you programmed it is that it would only be able to change a direct value inside the table, not a value of a value.
The second problem that ties into this is that the ChangeData
function is redundant, and it handicaps you from doing what you want to do.
You can accomplish much more with a simple
table[index] = value
Or, in your case,
data["Inside"]["Value"] = value
Hope I helped!
~TDP