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

Is it possible to check if a table index exists within the same block of code?

The issue I currently have is regarding table indexes. I use GetChildren() function to get a table, but I also want to check if the said index exists within the table or not. If so, the code executes. The thing is, if I use this method in the same IF statement, it only fires the first given condition that is TRUE (which makes sense obviously, but I want to check if other given indexes exist as well within the same block of code or statement.) I have found a way to check if an index exists, but to me, it's quite inefficient. Maybe looking at a code will help you understand what I am trying to achieve.

To clarify, this is a model, which has 6 members, so code in line 11 doesn't execute!

local Model = script.Parent:GetChildren()

if Model[1] ~= nil then
	print("Test1")
end

if Model[2] ~= nil then
	print("Test2")
end

if Model[7] ~= nil then
	print("Test3")
end

1 comment

0

you can do #Model to get the number of values in the table and only use certain indexes if they're less then or equal to the #Model

1 answer

instance:GetChildren() will return a list of an instance's children without any holes. Therefore, you can assume that indices 1 to #instance:GetChildren() all correspond to a non-nil value and all other keys don't exist.

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