Can't Get Value from Table Within Another Table

Hi, recently I was trying to tween GUI color by Color3.FromRGB() for a shop. I put a dictionary which has Colors but I can’t get it.

Error:image
Script:

local categoriesColors = {

    ["Effects"] = {CellBackground = Color3.fromRGB(0,113,0), ShopBackground = Color3.fromRGB(0,27,0), Other = Color3.fromRGB(0,245,0)};

    ["GamePasses"] = {CellBackground = Color3.fromRGB(170,0,127), ShopBackground = Color3.fromRGB(38,0,54), Other = Color3.fromRGB(183,0,255)};

    ["Items"] = {CellBackground = Color3.fromRGB(0,0,255), ShopBackground = Color3.fromRGB(0,17,52), Other = Color3.fromRGB(245,0,0)};

    ["Trails"] = {CellBackground = Color3.fromRGB(234,74,0), ShopBackground = Color3.fromRGB(49,0,0), Other = Color3.fromRGB(0,85,255)};
}
    for _, categ in pairs(shopCategories:GetChildren()) do
categ.MouseButton1Click:Connect(function()
	--Change Shop Colors--
	local tweenItemShowcase = tweenService:Create(shopGUI.ItemInfo.ItemShowcase, TI, {BackgroundColor3 = categoriesColors[categ].Other})
	local tweenTextLabel = tweenService:Create(shopGUI.ItemInfo.TextLabel, TI, {BackgroundColor3 = categoriesColors[categ].Other})
	local tweenItemsFrame = tweenService:Create(shopGUI.ItemsFrame, TI, {BackgroundColor3 = categoriesColors[categ].Other})
	local tweenShopBackGround = tweenService:Create(shopGUI.ShopBackground, TI, {ImageColor3 = categoriesColors[categ].Other})
	tweenItemShowcase:Play()
	tweenTextLabel:Play()
	tweenItemsFrame:Play()
	tweenShopBackGround:Play()
	
	--Set Categories' Items--
	setCateItems(categ)
end)
    end
    print(categoriesColors["Effects"].Other)

I printed I tried to make sure it returns me what I am expected and results made me more confused image

1 Like

Try this:
print(categoriesColors[“Effects”][3])

Don’t use string keys and use regular keys

@Ancient_Orange @FKAGamingDeOne I tried the both options but it still returns nil
It doesn’t find the Other

Whether you write ‘t.Effects’ or ‘t["Effects"]’ should not matter. These are both considered equivalent.


This code is mostly correct, there’s just a small error when you index categoriesColors causing you to attempt to index a nil value.

You index categoriesColors with categoriesColors[categ]. categ is an instance, not a string however categoriesColors only has string keys. This means the value of categoriesColors[categ] will always be nil.

Assuming your buttons are named the same as each category, you should instead be doing categoriesColors[categ.Name]. I suspect this will resolve your issue.

1 Like