How To Make a Responding NPC

Hello there developers!

Have you guys ever wanted to make an NPC that responds to a specific message that you send via the chat box? Well then your in luck, because a couple of months ago, I found out how you could script a responding NPC. Be sure to follow the steps down below:

  1. Create a script.

  2. Paste the following inside of the script:

- - Feel free to add more responses to the NPC!

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Hi!" then
			wait(1)
			game:GetService("Chat"):Chat(script.Parent.Head, "Hey there " .. player.name, Enum.ChatColor.Blue)
		end
	end)
end)

Here is how it came out:

I hope this resources helped you guys a lot! If it did, then feel free to leave some feedback down below! I would really appreciate it!

6 Likes

I made the responses table based so you don’t have to do a million elseif to check if the player said something

local Chat = game:GetService("Chat")
local Players = game:GetService("Players")
local responses = {
	["Hi!"] = "Hey there CooldudeGaming8905"
	
	--add whatever you want here
}

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local message = responses[msg]
		
		if message then
			task.wait(1)
			Chat:Chat(script.Parent.Head, message, Enum.ChatColor.Blue)
		end
	end)
end)

This is a tutorial, but if you want to have multiple messages, I recommend using my method as it uses tables instead of elseif

1 Like

Another cool option would be to add multiple answers to each prompt like -

local Players = game:GetService("Players")
local Chat = game:GetService("Chat")

local responses = {
	["Hi!"] = {
		"Hey there CooldudeGaming8905",
		"Hello!",
		"Greetings!"
	},
	
}


Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local message = responses[msg]

		if message then
			task.wait(1)
			local getRandomResponse = message[math.random(1,#message)]
			Chat:Chat(script.Parent.Head,getRandomResponse, Enum.ChatColor.Blue)
		end
	end)
end)

4 Likes

What

What do you mean by that?

- - Feel free to add more responses to the NPC!

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Hi!" then
			wait(1)
			game:GetService("Chat"):Chat(script.Parent.Head, "Hey there " .. player.name, Enum.ChatColor.Blue)
		end
	end)
end)

The way that you have it here you’d need a series of if statements to add more responses

- - Feel free to add more responses to the NPC!

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Hi!" then
			wait(1)
			game:GetService("Chat"):Chat(script.Parent.Head, "Hey there " .. player.name, Enum.ChatColor.Blue)
        elseif msg == "Bye!" then
         ...
         ...
	end)
end)

His method makes it so you have all ur responses in a nice table that is easily accessed and modified without alot of repeating code.