Help with indexing tables with variables as keys

I am trying to use 2 variables to index into a dictionary and I am SURE im not doing it right but cannot find any examples on how to do this on the wiki or other Lua help/docs.

I have a table set up as such:

--------// REGION Region_1A //---------
SpawnerDefs.Region_1A = {}

-- Type_1 SETTINGS
SpawnerDefs.Region_1A.Type_1 = {}
SpawnerDefs.Region_1A.Type_1.RespawnRate = 1
SpawnerDefs.Region_1A.Type_1.MaxRespawns = 0 -- leave at 0 for infinite respawns
SpawnerDefs.Region_1A.Type_1.Value = 1

From this table, I am trying to index the values for RespawnRate and MaxRespawns

here is my code:

local SpawnerDefs = require(ReplicatedStorage.GlobalDefs.SpawnerDefs)

local Spawner = {}
Spawner.__index = Spawner

function Spawner.new(thisPosition,thisType,thisRegion)
    local self = {}
    setmetatable(self,Spawner)
    
    -- these prints are for debugging, outputs below in this post
    print(thisRegion,thisType)
    print(SpawnerDefs)
    print(SpawnerDefs[thisRegion])
    print(SpawnerDefs[thisRegion].thisType)

    self.Position = thisPosition
    self.thisType = thisType
    self.thisRegion = thisRegion
    self.Defs = SpawnerDefs[region].thisType
    self.RespawnRate = self.Defs.RespawnRate
    self.MaxRespawns = self.Defs.MaxRespawns

    return self

end

My error from Studio is : ServerScriptService.Spawner.Spawner:25: attempt to index field ‘Defs’ (a nil value)

This error happen on the line above:
self.RespawnRate = self.Defs.RespawnRate

All i can think of is that I am not indexing properly, i set up my usual print statements as a test (seen above in the second code sample.

The output of the print statements are:
Region_1A Type_2
table: 00000257B9011380
table: 00000257B90113D0
nil

Can anyone see what I am doing wrong? How do I index deep into a table using two variables in place of keys?

Thanks!

1 Like

Replace line 19 with this.

self.Defs = SpawnerDefs[thisRegion][thisType]
3 Likes

wow that really threw me for a loop! It doesn’t really make sense to me to index without some sort of delimiter between those keys, but yeah! THANKS!