Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't my table value changing when there is another table inside of another?

Asked by 6 years ago
Edited 6 years ago

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.

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

0
Ah yes, I see what the problem is. I guess I can use a triplet to add additional indexes. Thank you very much for your help! :D WelpNathan 307 — 6y
Ad

Answer this question