Why is table/dictionary returning nil?

Hello, im making a little AI boi and im making table/dictionary (I really dont know the the difference) of words, but for some reason when I try to get a table/dictionary in the table/dictionary it returns nil saying “Table Expected Got Nil”. Please help

Code:
local intents = {
greeting = {
patterns = “hi”, “how are you”, “hello”, “good day”, “whats up”;
responses = “Hello”, “Good to see you again”, “Hey”, “Hi”;
context_set = “”
}
}

	for intent in pairs(intents) do
		for pattern in pairs(intent["patterns"]) do --// Errors here
			print("E")
		end
	end

Try turning the “patterns” into a table.

It is a table in side the table thats what im saying. I need to keep it in the intents table.

OH my bad dident realize that ok ill try

nope does not work, still returns nil value saying not a table

What I did:
local intents = {
greeting = {
patterns = {
“hi”, “hello”, “what up”, “hey”
};
responses = {
“Hey”, “Hello there”, “Nice to see you again”, “Hola”
};
context_set = {
“”
}
};
}

	for intent in pairs(intents) do
		print(intent)
		for pattern in pairs(intent["patterns"]) do --// Still says nil
			print("E")
		end
	end

I will get back to you in a minute. Let me just fix the script

How about saying

intent.greeting.patterns 

in the for loop.

no, I want to loop through all, Intent is = greeting when it loops wont make a difference. I have done prints and it works but it keeps saying pattern is not table and is nil. I think it may be since it still thinks its a string?

This is error more in depth
invalid argument #1 to ‘pairs’ (table expected, got nil) - Server - TalkMain:51

So umm, I think I fixed your script

local intents = {
	greeting = {
		patterns = {"hi", "how are you", "hello", "good day", "whats up"};
		responses = {"Hello", "Good to see you again", "Hey", "Hi"};
		context_set = {""}
	}
}

for _,intent in pairs(intents) do
	for _,pattern in pairs(intent["patterns"]) do 
		print("E")
	end
end

I only added _, before the variable in the for loops and it seems to work for me.

local intents = {
	greeting = {
		patterns = {"hi", "“how are you”", "“hello”", "“good day”", "“whats up”"},
		responses = {""," “Good to see you again”", "“Hey", "“Hi”"},
		context_set = {""}
	}
}

for intent in ipairs(intents.greeting.patterns) do
	print(intents.greeting.patterns[intent])
end

This works. I would always suggest you to use ipairs when iterating through arrays

1 Like

Thanks, so it looks like I was using the variable lol

1 Like