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:
Create a script.
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)
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
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)
- - 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.