Table value for value in table

Ok, so basically im having this problem where I need to take a value from the same table the variable that needs the value is in? Its hard to explain but heres the code and hopefully a decent explanation.

Character_Info = {
	Name = "Zooble",
	Character_ID = 3,
	AI_Strength = gameFunctions.ai_Levels[Character_Info.Character_ID], -- need to get the character_id variable inside this table
	Time_Frame = 2
        }

So I want to take the character id from the table and use it for the AI_Strength variable, problem is that whenever I try to do this i get hit with the attempt to index nil with 'Character_ID' error. Ive tried finding a solution online but I just can’t find the right words to describe it. Is this possible?

It’s because the table hasn’t been initizialised yet. I recommend just putting 3 or get a main variable like: local id = 3 then do:

local id = 3
Character_Info = {
	Name = "Zooble",
	Character_ID = id,
	AI_Strength = id
	Time_Frame = 2
        }
2 Likes

Try doing this:

Character_Info = {
	Name = "Zooble",
	Character_ID = 3,
	AI_Strength = nil, -- need to get the character_id variable inside this table
	Time_Frame = 2
        }
Character_Info.AI_Strength  = gameFunctions.ai_Levels[Character_Info.Character_ID]

or as @DeEchteBelg says do, you can create a variable, since you cant read a table, value when the table itself hasnt been initialised.

local char_id = 3
Character_Info = {
	Name = "Zooble",
	Character_ID = char_id ,
	AI_Strength = char_id, -- need to get the character_id variable inside this table
	Time_Frame = 2
        }
2 Likes

Thank you both! I was looking for a solution that would let me keep the rest of the script the same so the first one works perfectly!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.