NPC mimics human conversation

I would like to create an NPC script that enables the NPC to provide instant responses to anyone talking to them via roblox chat. Additionally, a chat answer will appear above their head displaying intelligent replies, not in general chat. However, currently, none of these functionalities are functioning properly.
I need Chat Service, not through dialog object.
DevForum didn’t help.

local Chat = game.Chat
local Object = game.Workspace.Part
local Message = "Hello"
local ChatColor = Enum.ChatColor.White
Chat:Chat(Object, Message, ChatColor)

I have tried next code:

local NPC = game.Workspace.NPC

local function NPCChat(message, color)
    Chat:Chat(NPC.Head, message, color)
end
3 Likes

Why not try this:
local message = messages[math.random(1,#messages)]
Seeing as you gave math.random a single variable, it likely doesn’t return anything other than the table’s index. That’s the only issue I see with this script.

1 Like

i also just noticed on studio 4th line has unknown global Chat…
And NPC doesn’t communicate with me

2 Likes

Actually yeah, nice spotting - Thought you already had a variable for Chat that wasn’t already referenced.
I’ll try this code myself and report back with the solution. It seems like a bit of a conspicuous but easily looked over solution…

1 Like

Okay, yep - It works as intended. Add a variable called Chat and refer it to game:GetService(“Chat”) before NPCChat and it should work. Make sure to add a debounce though (I’ve added it to the final script).
Final script:

local NPC = game.Workspace.NPC
local Chat = game:GetService("Chat")
local function NPCChat(message, color)
	Chat:Chat(NPC.Head, message, color)
end

local messages = {
	"Hello there!",
	"How are you doing today?",
	"It's a beautiful day, isn't it?",
	"What brings you here?",
	"Is there anything I can help you with?"
}

local db = false
local function NPCRespond()
	if db == false then
		db = true
		local message = messages[math.random(1,#messages)]
		NPCChat(message, Enum.ChatColor.White)
		task.delay(1,function()
			db = false
		end)
	end
end

NPC.HumanoidRootPart.Touched:Connect(NPCRespond)

Thank you for the clarification. I apologize for any confusion. Just to make it clear, I intended to mention that the previous discussion focused on NPC responses to player touches rather than player chat messages. The specific requirement is to have the NPC respond when a player talks in the chat and is in close proximity to the NPC model. I apologize for not mentioning this earlier. Sorry again

The Chat module is not designed for direct use in creating chat messages. Instead, you should use the ChatService module to handle chat messages and interactions.

Ohhhh, so you mean chatting through normal player chat? If so, I would probably refer you to this dev forum post: Making an NPC chat, with chat bubbles
It’s a similar post but shows how you can require a necessary chat service for the legacy chat to create and get speakers to chat channels which can be controlled by scripts inside of the NPC.

I’m not sure what else you would mean, and I’m not entirely sure what alternative is used for the newer chat, but I hope this helps & hopefully you can find what you’re looking for.

I just realized that is literally what I just posted.
I am incredibly sorry I didn’t even realize lol
but yeah - If you wish to make NPCs SPEAK IN CHAT, use the ChatService module. if you wanna do just chat bubbles, use Chat.

2 Likes

So what should script look like if were talking about npcs speak in chat, when player just saying anything? or “ai” npc?

Example could be from the game “Your sus Rommate” when you talk to ‘Catalina’

If you wanted NPCs to respond to chat but not actually speak using ChatService, you could make a script in ServerScriptService that fires an event to the NPC(s) to tell them “Hey! You can speak now”. and it displays a bubble chat.

An example for this is something like:

local Players = game:GetService("Players")
function InitChatted(plr)
plr.Chatted:Connect(function(msg)
--MAKE SURE TO ADD FILTERING TO THIS IF YOU WANT THE NPC TO REFLECT THE MESSAGE!!!
game.ServerStorage.Events.NPCRespond:Fire(msg)
end)
end

Players.PlayerAdded:Connect(function(plr)
InitChatted(plr)

end)

for _,plr in pairs(Players:GetPlayers()) do
InitChatted(plr)
end

Then, adding this to the NPC script:

game.ServerStorage.Events.NPCRespond.Event:Connect(function(msg)
NPCRespond() --You could add the "msg" variable to this function's variables and be able to respond to specific message strings. If you want.
end)

I’m not sure if this is exactly what you meant, But I hope this helps.
Events - Folder in ServerStorage.
NPCRespond - BindableEvent in Events.

1 Like

Something like from the game “Your sus Rommate” when you talk to ‘Catalina’

Could you provide an example video of what that game’s interactions with the player are like??

though, please note that dev forum members won’t code everything for you. If you’re interested in getting people to do the coding for you, I’d advise you to use the Talent Hub.

chrome_iIATgdpiGD
chrome_sRzyfdHpup

I would once again recommend you take a look at the scripts I’ve posted in this post alongside the aforementioned forum post about using the ChatService module.

It looks as if the game uses the ChatService module to communicate with other players in the same channel, alongside using bubble chat. They also seem to respond to player messages, which means they likely use a similar or exactly the same way of getting player’s chat as I previously gave you a base to.

That is as much as I can help you. I’ve given you the resources to research and analyze the scripts for yourself - Including a post showing you how ChatService works, using events to create server-to-server requests, and being able to create bubble chat.

Remember, the Dev Forum is not a place to ask people to script for you. We can only advise you in the right direction.