How to detect a NPC Message Chatted

What is the problem?

I attempted to use the “Chatted” function when an NPC speaks,
I made GUI similar to a game called “Regretevator.” I created a template GUI to identify NPC messages and display them as text. When I tested it, I encountered an error stating,
"Chatted is not a valid member of Model “Workspace.Elevator.NPC.NPC2.” I tried to fix it and it doesn’t work.



image

Client

local rp = game:GetService("ReplicatedStorage")
local npcEvent = rp:WaitForChild('NPCEvent')
local main = script.Parent

for i, v in pairs(main.ViewPortFolder:GetChildren()) do
	local npc = workspace.Elevator:WaitForChild(v.Name)
	local npcsPath = workspace.Elevator.NPC:WaitForChild(v.Name)
	
	for i2, v2 in ipairs(npc:GetChildren()) do
		local camera = Instance.new("Camera", v)
		v.CurrentCamera = camera
		local NPC = rp.NPCModel:FindFirstChild(v2.Name):Clone()
		NPC.Parent =  v.WorldModel
		NPC:PivotTo(CFrame.new(0,0,0))
			
		camera.CFrame = CFrame.new(NPC.Head.Position + Vector3.new(0,0,-4), NPC.Head.Position)
		npcEvent:FireServer("NPCDialouge", v2, npcsPath)
	end
end

Server:

local rp = game:GetService("ReplicatedStorage")
local chat = game:GetService("Chat")
local npcEvent = rp:WaitForChild("NPCEvent")
local dialougeConfig = require(rp:WaitForChild("DialougeConfig"))

local function Template(plr, message)
	local Dialouge = plr.PlayerGui:WaitForChild("Dialouge")
	local Main = Dialouge:WaitForChild("Main")
	local Template = Main:WaitForChild("TemplateFolder")
	local ScrollingFrame = Main:WaitForChild("ScrollingFrame")
	
	for i, v in pairs(Template:GetChildren()) do
		for i2, v2 in pairs(workspace.NPC:GetChildren()) do
			if v.Name == v2.Name then
				local templateClone = v:FindFirstChild(v2.Name):Clone()
				templateClone.Parent = ScrollingFrame
				templateClone.Text = message
			end
		end
	end
end

npcEvent.OnServerEvent:Connect(function(plr, eventType, arg1, arg2)
	local char = plr.Character or plr.CharacterAdded:Wait()
	if eventType == "NPCDialouge" then
		local NPCValue = arg1
		local npcName = arg2
		
		NPCValue.Value = true
		
		chat:Chat(npcName.Head, dialougeConfig[npcName.Name])
		
		npcName.Chatted:Connect(function(message) -- The Problem / Error
			Template(plr, message)
		end)
	end
end)

Reference:

I don’t know if i could find a solution for this

Chatted is not a valid member of a Model. You would want to use chat.Chatted instead: Chat | Documentation - Roblox Creator Hub

I would also recommend moving your Chatted event outside of the remote event unless you are planning on disconnecting it, otherwise it will keep connecting the same event.

How do you use chat.Chatted, does it’s have automatically parameter?

Yes. If you checked the link I provided to you, it automatically passes the part that you use Chat:Chat() on, along with the message.

1 Like

Ohh, thank you. i will try it now.

It’s working!

1 Like

Would you be willing to share your updated code?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.