AI Script broken

I’m trying to make a script that makes an Npc talks with chosen answers it works when I set the answer. But when I say it twice it throws this error: Workspace.Dummy.Script:5: attempt to perform arithmetic (add) on nil and number this is my script:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local answerTime = 0;
		local answer1,answerTime2 = message:match("addAnswer%[(.-)%:.-%]()",answerTime)
		answerTime = answerTime2 + 1
		local savedAnswer1 = answer1
		local otherAnswerTime = 0;
		local answer2,otherAnswerTime2 = message:match("addAnswer%[.-%:(.-)%]()",otherAnswerTime)
		otherAnswerTime = otherAnswerTime2 + 1
		local savedAnswer2 = answer2
		if message:find(savedAnswer1) then
			game:GetService("Chat"):Chat(script.Parent.Head,savedAnswer2,Enum.ChatColor.White)
		end
	end)
end)
1 Like

No answers ;-; (30 characters).

1 Like

answerTime2 is not being set to anything, what should it be set to?

1 Like

Fixed my previous error, but now theres this: Workspace.Dummy.Script:13: bad argument #2 (string expected, got nil) code:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local answerTime = 0;
		local answerTime2 = 0;
		local answer1 = message:match("addAnswer%[(.-)%:.-%]()",answerTime)
		answerTime = answerTime2 + 1
		local savedAnswer1 = answer1
		local otherAnswerTime = 0;
		local otherAnswerTime2 = 0;
		local answer2 = message:match("addAnswer%[.-%:(.-)%]()",otherAnswerTime)
		otherAnswerTime = otherAnswerTime2 + 1
		local savedAnswer2 = answer2
		if message:find(savedAnswer1) then
			game:GetService("Chat"):Chat(script.Parent.Head,savedAnswer2,Enum.ChatColor.White)
		end
	end)
end)
1 Like

savedAnswer1 is not being set to anythingl, what should it be set to?

Sorry for my incompetence but, I know it has to be set to the string but strangely it wont let me access it. I don’t exactly get string matching because I reverse-engineered it from an open source string matching function, that is somewhat like admin.