Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix my table code? It gives me an attempt to index field 'Y' a nil value

When I run the script, instead of getting "Hmm" output, i get an error: attempt to index field 'Y' (a nil value).

This is my code:

local D = {};
local E = {};
E["Y"].Test = "Hmm"
table.insert(D,E);
print(D[1]["Y"].Test);

1 answer

✓ Accepted answer

The code errors because Y doesn't exist, you need to create it before assigning something to it, it's like trying to change the property of something that doesn't exist, first the instance needs to be created in order to be changed.

local D = {};
local E = {};
E.Y = {} -- Create Y
E["Y"].Test = "Hmm"
table.insert(D,E);
print(D[1]["Y"].Test);

1 comment

0

I see.. thank you! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.