Chat Function Error

Hi, i was making a npc system but i got an error which the head of my npc has to be in workspace, even tho its parent was in workspace, i have no clue on how to fix this

also here’s the script i got the error from:

local ReplicatedService = game:GetService("ReplicatedStorage")
local ChatService = game:GetService("Chat")

local NPCSpawn = workspace.CustomerSpawn
local NPCFolder = workspace.NPCS

local function MoveTo(humanoid : Humanoid, targetPoint, andThen)
	local TargetReached = false
	
	local connection
	
	connection = humanoid.MoveToFinished:Connect(function(reached)
		TargetReached = true
		connection:Disconnect()
		connection = nil
		if andThen then
			andThen()
		end
	end)
	
	humanoid:MoveTo(targetPoint)
	
	task.spawn(function()
		while not TargetReached do
			if not (humanoid and humanoid.Parent) then
				break
			end
			
			if humanoid.WalkToPoint ~= targetPoint then
				break
			end
			
			humanoid:MoveTo(targetPoint)
			task.wait(6)
		end

		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

while task.wait(5) do
	local Customers = ReplicatedService.Customers
	local RandomNPC : Model = Customers["Customer"..math.random(1, #Customers:GetChildren())]
	
	if #NPCFolder:GetChildren() < 1 then
		local NPC = RandomNPC:Clone()
		local NPCSize = NPC:GetExtentsSize()
		
		NPC.Parent = NPCFolder
		NPC:PivotTo(NPCSpawn.CFrame + Vector3.new(0, NPCSize.Y/2 + NPCSpawn.Size.Y/2, 0))
		NPC.PrimaryPart.Anchored = false
		
		MoveTo(NPC:WaitForChild("Humanoid"), workspace.CustomerDestination.Position, function()
			local DialogPrompt = workspace.Dialog.ProximityPrompt
			local Head = NPC:WaitForChild("Head")
			
			DialogPrompt.Enabled = true
		    DialogPrompt.Triggered:Connect(function(player)
				ChatService:Chat(Head, "hi")
				
				task.wait(1)
				ChatService:Chat(Head, "You are very cool so have 100 mony and bye")
				task.wait(3)
				
				NPC:Destroy()
				require(game:GetService("ServerScriptService").Modules.ProfileManager).PlayerProfiles[player]:GiveMoney(100)
				DialogPrompt.Enabled = false
			end)
		end)
	end
end

if not Head.Parent then return end
Put that in the DialogPrompt.Triggered function as the first line.

So, it was in workspace but then it was nil?

and… it worked! thanks alot @SubtotalAnt8185, i would rework on a entire different system to just make a npc talk and be destroyed, but with a simple line, i prevented hours of work!

yeah, because the prompt thought the head still existed but i didnt.

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