Type Error: Cannot add indexer to table

Hello. For context, I’ve set my scripts as strict.
I’m trying to access a dictionary and set a variable to a table inside that dictionary:

partyService.returnFirstAnatomy = function(entity: string, entityType: string)
	type x = {[string]: {}}
	local baseAnatomy: x = {} :: x
		
	if entityType == "Party" then
		baseAnatomy = entityInfo.partyInformation[entity].Anatomy
	elseif entityType == "Enemy"  then
		baseAnatomy = entityInfo.enemyInformation[entity].Anatomy
	end
end

However, on lines: baseAnatomy = entityInfo.partyInformation[entity].Anatomy and baseAnatomy = entityInfo.enemyInformation[entity].Anatomy, I received a type error mentioned in the title of this post. Here are the two modules I’m referencing inside of this script:

entityInfo.partyInformation = {
	["Player"] = {	
		Anatomy = {
			["Head"] = {Health = 3; PerformAction = false};
			["LeftArm"] = {Health = 5; PerformAction = true};
			["LeftLeg"] = {Health = 5; PerformAction = false};
			["RightArm"] = {Health = 5; PerformAction = true};
			["RightLeg"] = {Health = 5; PerformAction = false};
			["Torso"] = {Health = 10; PerformAction = false};
		};
		Abilities = {
			["Punch"] = {
				Performers = arms;
				Class = "Offensive";
				Scale = 5;
			};
			["Block"] = {
				Performers = arms;
				Class = "Defensive";
				Scale = 5;
			};
		};
	};
};
entityInfo.enemyInformation = {
	["Stone Golem"] = {
		Anatomy = {
			["Head"] = {Health = 5; PerformAction = false};
			["LeftArm"] = {Health = 15; PerformAction = true};
			["LeftLeg"] = {Health = 10; PerformAction = true};
			["RightArm"] = {Health = 15; PerformAction = true};
			["RightLeg"] = {Health = 10; PerformAction = true};
			["Torso"] = {Health = 25; PerformAction = false};
		};
		Abilities = {
			["Slam"] = {
				Performers = arms;
				Priority = arms;
				Class = "Offensive";
				Scale = 3
			};
			["Kick"] = {
				Performers = legs;
				Priorty = {"Torso", "LeftArm", "RightArm"};
				Class = "Offensive";
				Scale = 4
			};
			["Block"] = {
				Performers = arms;
				Priority = {"Head"};
				Class = "Defensive";
				Scale = 0
			}
		};
		Immunities = {
			["Bleed"] = entireAnatomy;
			["Wither"] = entireAnatomy;
		};
		Vulnerabilities = {};
	};
}

I’ve tried rewriting type x and local baseAnatomy, but they still throw out the same type error.
Any help is appreciated. Thank you for reading.

these doesn’t matter.

you’d access this part by entityInfo.partyInformation.Player
or if your entity == "Player" then entityInfo.partyInformation[entity] would be available

so you mixed up quite a few places here. you checked entityType but you used entity for the index
even if entityType is the correct one, there is only Player . no Party in the table

same with the Enemy vs Stone Golem

This doesn’t change anything? They are getting “Player” not “Party” (or Enemy and Stone Golem) from the table, entityInfo.partyInformation[entity] usage is for multiple entities of the same kind as I assume these won’t be the only entities here.

1 Like

Is it because you specified an emtpy array in teh type?
(Im on mobile rn I can’t check)

Try changing the type to:

type x = {[string]: {any}} --add any?

Because it might think the array is empty.
Hope this helps.

That’s true. Although, I still want to fix this type error in my script and learn how to prevent it from happening again.

You might be misconstruing what I am trying to do. I’m accessing a different table depending on the entityType, and entity exists only to find the exact entity after I know which table to search in.

To reiterate, I am checking if entityType is either "Party" or "Enemy", so the script knows which table to select. entity is the name of the entity itself, which in this instance could be either "Player" or "Stone Golem".

1 Like

I’m still experiencing the same problem, unfortunately. :pensive:

I remade your setup in studio and it seems like changing the type definition to a dictionary works:

type x = {[string]: {[string]: any} -- dictionary of dictionaries
--probably works because it it the closest to your actual type

hope this helps!
here is the my setup btw:

--!strict
local info = {
	["Player"] = {
		Anatomy = {
--some property
			["Head"] = {Health = 3, Perform = false},
			["Head2"] = {Health = 3, Perform = false},
			["Head3"] = {Health = 3, Perform = false},
		},
		Abilities = {
		
		}
	}
}

local function f(n: string, t: string)
	type x = {[string]: {[string]: any}}
	local baseAnatomy : x = {} :: x
	
	baseAnatomy = info["Player"].Anatomy
end
2 Likes

This only works, but I am trying to access the tables with entity as a key, rather than "Player" or "Stone Golem" as my key.

but entity is a string no?

= function(entity: string, entityType: string)

so it should work either way.
otherwise just replace the second string in the dictionary type def to the type of entity:

type x = {[string]: {[yourtype]: any}}

edit: you can always include a full custom type definition:

export type Limb = {
   Health: number,
   PerformAction: boolean
}

export type Ability = {
     Performers: any,
     Priority: any?,
     Class: string,
     Scale: number
}

export type Anatomy = {
    Head: Limb,
    LeftLeg: Limb,
    RightLeg: Limb,
    LeftArm: Limb,
    RightArm: Limb,
    Torso: Limb
}

export type AbilitySet = {[string]: Ability}

export type EntityInfo = {
     Anatomy: Anatomy,
     Abilities: AbilitySet
     --add other members
}

edit:
and then use the type;

local base : Anatomy = {} :: Anatomy
--checking
base = entityInfo.partyInformation[entity].Anatomy--properties must match exactly.

It is, which is why I’m confused the script still throws a type error.

I tried your solution, but it still throws the same error for some reason. Thank you for your time regardless. :pray:

1 Like