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.
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: