Indexing a table with a number returns nil

I am writing framework which would require me to index tables to acquire a value within them, but I cannot seem to index a table which is holding other tables.

Hey everyone! I’m currently in the midst of programming the systems for an upcoming project of mine, and one of the necessities will be accessing tables stored by modulescripts.

However, upon implementation of this, I realized that indexing would be… a little tedious.

Here’s an example of my problem:

-- MODULE

return function()

local infoMain = {
-- data would go here
["TBL"] = {
Id = 1;
};
};

return infoMain

end

-- acquisition:

local returnedTBL = require(module)()

print(returnedTBL[1].Id) -- prints nil
print(returnedTBL[1]) -- also prints nil

If anybody with more programming experience than me can chime in or provide support, it’d be greatly appreciated.

Thanks!

1 Like

The issue with this is… each table within the returned data will have a different name, so in order to change the actual index to not be an integer, I would have to access the name of the actual index itself through a labyrinth of Serverside architecture… however, I’ll wait for other people’s inputs.

Ah, I didnt read the post, my bad. Hold on.

I don’t understand the a problem.

Are you saying that each module returns a table with one key having a different name?

Maybe next(t) would be of interest?

local t1 = { ABC = 123 }
local t2 = { DEF = 456 }

local key, value = next(t1) -- ABC, 123
local key2, value2 = next(t2) -- DEF, 456

Essentially, yes. Each table is holding the keys and the corresponding data for magic abilities.

And each ability has its own name, which is the actual index of the table, hence why its a string.

I have never utilized next before, but I will look into it for this scenario.

Ah, if there are multiple keys, would maybe a for-loop work for you? It would loop over the table and give you the values in pairs of the key (ability name), and table (ability data)

local abilities = {
    Punch = { Id = 23 },
    Fire = { Id = 45 }
}

for abilityName, abilityInfo in abilities do
    print(abilityName, abilityInfo) -- Would return for each ability in the list
    print(abilityInfo.Id)
end

I have tried a for loop yes, but it never actually accessed the necessary values. It was my first idea.

Here’s the code I’m accessing, for easier reference:

local infoMain = {}
	infoMain.Index = 1
	infoMain.Cost = 100
	infoMain.Name = "Fire"
	infoMain.ColorTheme = Color3.fromRGB(855, 133, 58)
	infoMain.SelectedGradient = ColorSequence.new(Color3.fromRGB(255, 0, 0),Color3.fromRGB(255, 116, 52))
	infoMain.Image = "rbxassetid://12382783870"
	infoMain.TransparentImage = "rbxassetid://12382783870"

	local Abilities = {}

	local bullets = {}
	bullets.Id = 1
	bullets.MagicType = "Fire"
	bullets.Name = "Blazing Barrage"
	bullets.Description = "User summons a multitude of flaming bullets."
	bullets.Cost = 0
	bullets.EnergyCost = 20
	bullets.Cooldown = 4
	bullets.Type = "Bullets"

	bullets.ClientFunction = function(plr,callback)
	
	end
	
	Abilities["Blazing Barrage"] = bullets
	
	-- netx move
	
	local blast = {}
	blast.Id = 2
	blast.MagicType = "Fire"
	blast.Name = "Blazing Fury"
	blast.Description = "User summons a large fireball capable of doing heavy damage."
	blast.Cost = 300
	blast.EnergyCost = 1
	blast.Cooldown = 7
	blast.Type = "Blast"
	
	blast.ClientFunction = function(plr,callback)
	
	end
	
	Abilities["Blazing Fury"] = blast
	
	-- next move
	
	local TP = {}
	TP.Id = 3
	TP.MagicType = "Fire"
	TP.Name = "Flame-rush"
	TP.Description = "User quickly dashes forward in a ball of flames, stunning victims."
	TP.Cost = 500
	TP.EnergyCost = 1
	TP.Cooldown = 7
	TP.Type = "Teleport"
	
	TP.ClientFunction = function(plr,callback)
		
	end
	
	Abilities["Flame-rush"] = TP
	
	-- next move
	
	local AOE = {}
	AOE.Id = 4
	AOE.MagicType = "Fire"
	AOE.Name = "Flame Rain"
	AOE.Description = "User summons three balls of flames on a position they wish to bombard."
	AOE.Cost = 500
	AOE.EnergyCost = 1
	AOE.Cooldown = 7
	AOE.Type = "AOE"
	
	AOE.ClientFunction = function(plr,callback)
		
	end
	
	Abilities["Flame Rain"] = AOE
	
	-- next move
	
	local Ultimate = {}
	Ultimate.Id = 5
	Ultimate.MagicType = "Fire"
	Ultimate.Name = "Flaming Beam"
	Ultimate.Description = "User summons a rain of fireballs on their target."
	Ultimate.Cost = 500
	Ultimate.EnergyCost = 1
	Ultimate.Cooldown = 7
	Ultimate.Type = "Ultimate"
	
	Ultimate.ClientFunction = function(plr,callback)
		
	end
	
	Abilities["Flaming Beam"] = Ultimate
	
	infoMain.Attacks = Abilities

	return infoMain

local tbl = require(module)()

for _,v in pairs(tbl.Attacks) do
print(v.Id) -- prints wrong data or nil
end

Correct me if I’m wrong, but doesn’t your module return infoMain? To access the abilities you’d need to iterate over infoMain.Attacks instead…

Edit: Also, why are you calling the return value of require(module)?

Yes, it does reutrn infoMain as the returned value. However, I intialized infoMain.Attacks = Abilities, which should’ve set .Attacks to be the table holding all the abilities.

“Edit: Also, why are you calling the return value of require(module)?” - I’m sorry I don’t quite get what you mean. Do you mean why I’m returning a function or?

Because the table that is being returned is a dictionary which uses strings as keys and not numbers.

Oh, I see. My bad I thought you looped over infoMain, I misread that.

Can you show us the output of the example you showed before?

EDIT: Also try printing _ and v.

Yes, but they are using pairs which works for string keys.

I don’t see where OP is using this

Right at the bottom, here’s what it says:

Okay, I just reiterated through my code and realized it was a mistake on my end. I’m gonna mark your iterator statement as the solution. Thanks for your time though, I really appreciate it.

2 Likes

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