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

Is it possible to refer to the 'parent' table?

I'm trying to create a simple program that has a table with indices that can refer to it's parent table. I realize you can create an empty table first, then just create new keys while having the reference to the table. Example:

local t = {}
t.x = 1
t.y = t.x -- Refer to table 'x' for a value inside table 't'

However, I'm wondering if it's possible to do this during the table initialization. In other words, assigning key/value pairs in the table up-front instead of giving it a new index later in the code. Abstract example:

local t = {
	x = 1;
	y = t.y -- Refering to 't', inside of 't', for index 'y'
}

The code above is just an example, I know why it won't actually work. So I tried creating a reference to t using local before assigning it a value, so I could possibly access it during it's declaration. This is what confuses me...

local t -- Reference to t for later in the code, even though t is nil for now.

t = {
	x = 1;
	y = t.y -- This time t has been defined outside this table, so it should work?
}

The code above is what I expected to work, however it still reads 't' as nil inside the table. I'm not quite sure what else to try, or if it's even possible (even though I'm pretty sure it is). But if any of you see something I'm not getting, I'd appreciate the insight!

2 answers

✓ Accepted answer

There are a couple of reasons why this would not work. The first reason being you are trying to assign a variable to itself.

This is the equivalent of doing y = y, which is not possible.

Secondly

You are trying to assign a variable to the same variable before it has even been defined. In a table, you can never assign a variable to another variable in the table, which means even doing this wouldn't work:

--JSON operation functions
local httpserv = game:GetService("HttpService")
function EncodeJSON(dat) return httpserv:JSONEncode(dat) end
function DecodeJSON(dat) return httpserv:JSONDecode(dat) end

local t

t = {
	x = 1,  
	y = 1, 
	z = t.y
}

print(EncodeJSON(t))  --Still return nil

But why does it still return nil

In your third example, t is not being defined at the beginning. It is just represented as nil, not as a table or a string. This means that in line 3, t has to be defined as a table, then add the variables.

Doing what you did will cause the script to error saying that t is nil. The way around it saying nil is simply defining t as a table, and then adding in the variables

--JSON operation functions
local httpserv = game:GetService("HttpService")
function EncodeJSON(dat) return httpserv:JSONEncode(dat) end
function DecodeJSON(dat) return httpserv:JSONDecode(dat) end

local t = {} --Now it's defined as a table

t = {
	x = 1,  
	y = 1, 
	z = t.y --Still doesn't add to table
}

print(EncodeJSON(t)) --It only returns a table with x and y in it

The way you would incorporated z into the table, would be defining it after the table has been created

--JSON operation functions
local httpserv = game:GetService("HttpService")
function EncodeJSON(dat) return httpserv:JSONEncode(dat) end
function DecodeJSON(dat) return httpserv:JSONDecode(dat) end

local t = {} --Now it's defined as a table

local t = {}

t = {
	x = 1,  
	y = 1, 
	
}

t.z = t.y

print(EncodeJSON(t)) --This will return x, y, and z

I hope this was what you were looking for!

The Parent table doesn't exist
It's like a magician's hat. Both have bunnies.

At the time which the table is created, it is evaluated from the inside out. This means that at the time it is evaluated, t is nil. The table simply has no idea that it is being assigned to a value - The value is irrelevant - It only knows that it is being created as a literal.

There is no good solution for this, other than to look into a transpiled language which takes over the role of manually declaring and assigning by references.

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