Table not working

So today, I was trying to get a string value and put it in the table for I can grab it later but.
I have multiple values I want to put on the table. but when i try to print 1 it would say the text but if 2,3,4,5 its nill. is there any way to fix this

local function Find_NPC(Sting)
	for _, Grab in pairs(workspace:WaitForChild("Workspace"):WaitForChild("NPC"):GetDescendants()) do
		if "Name" == tostring(Sting) then
			if Grab:IsA("Model") and Grab:FindFirstChildWhichIsA("Humanoid") and Grab.PrimaryPart == Grab:FindFirstChild("HumanoidRootPart") then
				return Grab.Name
			end 
		elseif "Description" == tostring(Sting) then
			local List_Lines = {}
			if Grab:IsA("Model") and Grab:FindFirstChildWhichIsA("Humanoid") and Grab.PrimaryPart == Grab:FindFirstChild("HumanoidRootPart")then
				for _, lines in pairs(Grab.Description:GetChildren()) do
						if lines.Name == "Value1" then
						table.insert(List_Lines,1,Grab.Description.Value1.Value)
						return List_Lines
                        elseif lines.Name == "Value1" and lines.Name == "Value2" then
						table.insert(List_Lines,1,Grab.Description.Value1.Value)
						table.insert(List_Lines,2,Grab.Description.Value2.Value)
						return List_Lines
						elseif lines.Name == "Value1" and lines.Name == "Value2" and lines.Name == "Value3" then
						table.insert(List_Lines,1,Grab.Description.Value1.Value)
						table.insert(List_Lines,2,Grab.Description.Value2.Value)
						table.insert(List_Lines,3,Grab.Description.Value3.Value)
						return List_Lines
						elseif lines.Name == "Value1" and lines.Name == "Value2" and lines.Name == "Value3" and lines.Name == "Value4" then
						table.insert(List_Lines,1,Grab.Description.Value1.Value)
						table.insert(List_Lines,2,Grab.Description.Value2.Value)
						table.insert(List_Lines,3,Grab.Description.Value3.Value)
						table.insert(List_Lines,4,Grab.Description.Value4.Value)
						return List_Lines
						elseif lines.Name == "Value1" and lines.Name == "Value2" and lines.Name == "Value3" and lines.Name == "Value4" and lines.Name == "Value5" then
						table.insert(List_Lines,1,Grab.Description.Value1.Value)
						table.insert(List_Lines,2,Grab.Description.Value2.Value)
						table.insert(List_Lines,3,Grab.Description.Value3.Value)
						table.insert(List_Lines,4,Grab.Description.Value4.Value)
						table.insert(List_Lines,5,Grab.Description.Value5.Value)
						return List_Lines
						elseif lines.Name == "Value1" and lines.Name == "Value2" and lines.Name == "Value3" and lines.Name == "Value4" and lines.Name == "Value5" and lines.Name == "Value6" then
						table.insert(List_Lines,1,Grab.Description.Value1.Value)
						table.insert(List_Lines,2,Grab.Description.Value2.Value)
						table.insert(List_Lines,3,Grab.Description.Value3.Value)
						table.insert(List_Lines,4,Grab.Description.Value4.Value)
						table.insert(List_Lines,5,Grab.Description.Value5.Value)
						table.insert(List_Lines,6,Grab.Description.Value6.Value)
						return List_Lines					
					end
				end
				end
				
		  --elseif
		end
	end
end
local NPC_Description = Find_NPC("Description")
print(tostring(NPC_Description[1]))
print(tostring(NPC_Description[2]))
print(tostring(NPC_Description[3]))

Well, it’s actually a very simple mistake.

You have condition A and condition B. You’re checking if all meet condition A.

so

if A then
 --code
elseif A and B then
 --code
end

Since A is true, it’ll stop at the first condition. So you’d have to invert it

if A and B then

elseif A and C then

else if A then

end

Or just this

if A then
   --insert first value
   if B then
      --insert all values but 1st one
   end
   if C then
      --insert all values but 1st one
   end
end

Also, one tip, to save you lines, time, and mental sanity you can use loops to insert the values.

for i=1,6 do 
   table.insert(List_Lines, i, Grab.Description:FindFirstChild("Value"..i).Value)
end

-- replace 1 and 6 with start and ending numbers, if last example I gave then start from 2

Hope this helped,
Happy coding

1 Like

To add onto what he said he can just do

List_Lines["tableName-"..i]

instead of that long table.insert crap

Yeah, that works as well, but only if the array is empty. An advantage of table.insert is that it “moves” the other indexes