Code Review - Basic Chat Bot

Greetings,

I’m looking for ways to improve the code on my Chat Bot, Nora. Yes, this is open-sourced, only because I want to put it on Community Resources soon :stuck_out_tongue:

Game Link

Also this is one of my first DevForum topics (second i think), so please let me know how I can improve my posts if something seems wrong.

Thanks

Think of what you think would be the best decision for the bot.

And posts don’t really matter since 90% of the people don’t use the correct formatting for a post, unlike the other 10%. (from what I’ve noticed)

Hello!

It’s a good start however the only things I can say and receive a reply for is: Hello and hi. Is there anything else in the game apart from a response from those 2? However, its a nice start and you should develop on it. Good work.

Did I format my post okay?
(chars)

Thanks for the feedback! I’ll take that into consideration and make changes sometime.

Yea, as long as it is clear enough for people to understand.

Okay thanks
(chars chars chars)

Hello! I hope you’re doing well :slight_smile:

The way you organized the interactions and the interaction handler wasn’t the best because it’s harder to maintain, and it can easily get unorganized. I thought on this approach, take a look:

Interactions (ModuleScript) - Here is where we can keep all the interactions organized

local Interactions = {
	{
		Matches = { 'hi', 'hello', 'hey' },
		Responses = {
			"Whats up my dude";
			"Hey";
			"Yooo";
			"Greetings, friend.";
			"Hi";
			"Hewoooooooo";
		},
	},
	
	{
		Matches = { 'bye' },
		Responses = {
			"Noo don't leave me!!";
			"Bye!";
			"See you soon!";
			"i get lonely, you don't want to leave me do you?";
			"just press F4 already";
		},
	},
	
	{
		Matches = { 'thanks', 'tysm' },
		Responses = {
			"Oh did I do something?";
			"Your welcome!";
			"You are being really sincere today :)";
			"No problem!";
			":D";
		},
	},
	
	{
		Matches = { 'i hate you' },
		Responses = {
			"Why are you here??";
			"Please go away!";
			"Moveee!";
		},
	},
}

return Interactions

As you can see, each context is separated (“hi”, “bye”, “thanks” etc), creating an array. The Matches table stores the trigger words, if the player chats any word inside it, a random response will be sent back. The Responses table stores the possible responses, which will be randomly chosen later.

Now that we’ve understood how our interactions data is stored, let’s move on to the main script:

This is the way I parented the scripts, you can do it as you prefer.
image

MainScript (Script) - Here is where we put things together

local Interactions = require(script.Interactions)

local function GetResponseForInteraction(interaction: string): string
	-- Going through each interaction
	for _, interaction_information in ipairs(Interactions) do
		
		-- Looking for all the matches to see if we find a trigger word 
		for _, match in ipairs(interaction_information.Matches) do
			
			-- Does the interaction match?
			if (interaction:lower():match(match)) then
				-- Choosing a response and returning it
				local responses = interaction_information.Responses
				local random_response = responses[math.random(#responses)]

				return random_response
			end
		end
	end
	
	-- If no matches have been triggered, we return this message
	return "Ops! I can't understand it yet."
end


-- Variables
local Players = game:GetService('Players')
local NoraNPCModel = workspace:WaitForChild("Nora")
local Head = NoraNPCModel:WaitForChild("Head")
local Chat = game:GetService("Chat")


Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local response = GetResponseForInteraction(message)
		Chat:Chat(Head, response)
	end)
end)

I hope it helps! This is only one of many possible ways you can think to organize everything and keep it consistent. Good luck with your project! :slight_smile:

2 Likes

I now realised I didn’t read this, so sorry. Thanks for the help and this will be taken into deep consideration!

1 Like