Trying to count the elements from a table always returns 0

I have the following code in a ModuleScript, for a tutorial. It works fine to pull data from it. My problem is that trying to count the amount of items inside the table always returns 0. Did I do something wrong with the formatting of the table?

-- ModuleScript
local tutorial = {
	["1"] = {
		["Title"] = "Test";
		["Content"] = "David sucks at editing these c:<";
		["Beam"] = {script.TutorialBeam, Vector3.new(0,10,0)};
	},
	["2"] = {
		["Title"] = "Test #2";
		["Content"] = "it works!";
		["Beam"] = {script.TutorialBeam, Vector3.new(0,5,35)};
	};
}

return tutorial
-- LocalScript
local function beginTutorial()

	tutorialWindow.Visible = true

	local index = 0
	local maxIndex = #tutorialStages
	
	print(maxIndex) -- This always prints out as 0, regardless of the amount of items inside the table

	tutorialWindow.Commit.MouseButton1Click:Connect(function()
		index += 1
		local step = tutorialStages[tostring(index)] -- This actually works.
	end
end

Thanks in advance!

tutorialStages presumably does not contain integer indices. The unary length operator on tables only account for integer indices starting at 1.

"1" (as a string) isn’t considered an integer by Lua(u). Is there any reason why you’re using the string "1" as the index rather than the numeric 1?

https://www.lua.org/manual/5.1/manual.html#2.5.5

1 Like