Table starts indexing instead of setting a value?

So I created a UI managing module which allows me to add screenuis to it and automatically open them up with a key passed through. This is what the function looks like to add a new UI to the manager.

function UIManager:AddUI(UIName, UIObject, Hotkey, ShowCallback, HideCallback)
	self.UIs[UIName] = {Name = UIName, UI = UIObject, Hotkey = Hotkey, ShowCallback = ShowCallback, HideCallback = HideCallback, disabled = false};
end

Thing is, it works great for the first two UIs I add, which are the Inventory and Settings UI.

Settings UI

	self.Controllers.UIManager:AddUI("Settings", self.SettingsFrame.Parent.Parent, Enum.KeyCode.P, function()
		script.InventoryOpen:Play();
	end, function()
		script.InventoryOpen:Play();
	end)

Inventory UI

	self.Controllers.UIManager:AddUI("Inventory", self.InventoryUI.InventoryUI, Enum.KeyCode.G, function()
		script.InventoryOpen:Play();
	end, function() 
		script.InventoryOpen:Play();
	end)

But, when I try to add this other UI, a Faction menu, I get this error:

Players.WorldPosition.PlayerScripts.Aero.Controllers.UIManager:15: attempt to index nil with 'Factions'

Creating the tables inside self.UIs with the UIName has always worked, up until this point. For some reason it’s trying to index it, even though it should just be creating the table, like it did the past 2 times I used this function.

Here’s the code I’m using to try and add the Faction UI to the UI manager.

	self.Controllers.UIManager:AddUI("Factions", FactionMenu, Enum.KeyCode.K, function()
		script.Open:Play();
	end, function() 
		script.Open:Play();
	end)

I tested to see if what I thought is correct, by it is trying to index instead of create, and I think I am correct. I added

self.UIs["Test"] = {};

Which result in the same error. I’m confused as to why it’s able to create two values in self.UIs but after the third, it’s starts acting differently.

I have to guesses here:

  1. For the settings and the inventory UI, you did self.Something.Something, whereas for the factions you just put FactionMenu. Though, I highly doubt that this is the reason, but who knows?

  2. So, provided that you did the test with the self.UIs["Test"], it seems as if self.UIs is nil and it’s trying to index it with "Factions" (in the line self.UIs[UIName]). Indexing doesn’t create a new key unless if the object you’re doing the indexing on is not nil. Make sure self.UIs exists.

Hope that helps!

1 Like

Those both point to a UI, so I don’t think that’s it either.

I just now tested this, and yeah this was the problem, thanks. self.UIs wasn’t being created soon enough. Thank you!

1 Like