"Attempt to index nil with number" error in handling a dictionary

Hello fellow devs!
Now I am creating information boards like below,

error1

Once you click a board, the corresponding screen gui frame should be shown.
For that, I have made two scripts: One is a module script to manage the data,
and another local script to show/hide the screen gui.

The module script:

local InfoBoards = {}

--Store CSV in a table and blocks
function InfoBoards.Merchant(key)
	local csv = 
		[[
		NagayaToilet,Toilet and Garbage Dump,10732266535
NagayaWell,Well Place of Nagaya,10732805315
FireLookoutTower,Fire Lookout Tower,10755886826
NagayaApartment,Nagaya Apartment,10753819113
Sushi,Sushi,10930906724
Tempura,Tempura,10941616064
		]]

	local linePattern = "[^\r\n]+"
	local csWordPattern = "[^,]+"
	local rows = {}


	for line in string.gmatch(csv, linePattern) do
		local row = {}

		for word in string.gmatch(line,csWordPattern) do
			table.insert(row, word)
		end

		table.insert(rows, row)
	end
	
	local sheet = rows
	
	InfoBoards.title = sheet[tostring(key)][2]
	InfoBoards.assetId = sheet[tostring(key)][3]

	return InfoBoards.title, InfoBoards.assetId
	
end


return InfoBoards

The local script:


local InfoBoards = require(game.ReplicatedStorage.InfoBoards)

local infoBoardsObjects = game.Workspace.InfoBoards
local infoBoardGui = script.Parent.InfoBoardGuiTemp

for _, eachCategory in pairs (infoBoardsObjects:GetChildren()) do
	if eachCategory.ClassName == "Folder" then
		for _, eachBoard in pairs(eachCategory:GetChildren()) do
			if eachBoard.ClassName == "Model" then
				eachBoard.Main.ClickDetector.MouseClick:Connect(function()
					if eachCategory.Name == "Merchant" then
						local title, assetID = InfoBoards.Merchant (eachBoard.Name)
						
						infoBoardGui.Title.Text = title
						infoBoardGui.ScrollingFrame.ImageLabel.Image = "rbxassetid://"..assetID
						
						infoBoardGui.Visible = true
						
						if infoBoardGui.CloseButton.MouseButton1Click:Connect(function()
								infoBoardGui.Visible = false
							end) then
						end
						
					end
				end)
			end
		end
	end
end

When I test these scripts, “attempt to index nil with number” error is shown as the first pic.
The following line returns the error:

InfoBoards.title = sheet[tostring(key)][2]

I don’t know what is wrong with this.
I appreciate if someone point out the problem!

You’re treating sheet like a dictionary, but it’s not a dictionary. If you use natch instead of gmatch, you could make it a dictionary. Alternatively you could loop through the rows and check if row[1] = key.

1 Like

Now the scripts work beautifully. Thank you so much for your help!!

1 Like