Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Iterating through a metatable __index metamethod? [ANSWERED]

Hey, I was learning about metatables and metamethods and I was trying to iterate through an originally empty array but having values assigned via __index metamethod.

Here's my code:

local codes = {}
setmetatable(codes,{
	__index = {"Hello World","1337"},
	__metatable = function()
		getmetatable(codes)
	end
})


script.Parent.MouseButton1Click:Connect(function()
	for i,v in pairs(codes) do
		print(v)
	end
end)

When I clicked nothing gets printed; please help.

1 answer

Credit to milk destroyer and madhoes for finding the solution.

I was iterating through the codes array but nothing was in it; to solve my question the correct solution was to iterate through the __index array.

The code would look like this:

local codes = {}
local meta = {
	__index = {"Hello World","1337"},
}

for i,v in next,meta.__index do
	-- code
end

This discussion is preserved as it was. New answers, comments, and votes are closed.