NPC Movement Not Processing

I have made a script to handle NPC movement for my burger game, my NPC stay in the same place when spawned.

Error Returned

Unable to cast Array to Vector3  -  Server - NPCSetup:56
  15:39:24.104  Stack Begin  -  Studio
  15:39:24.104  Script 'ServerScriptService.NPCSetup', Line 56  -  Studio - NPCSetup:56
  15:39:24.104  Stack End  -  Studio

Script

local Storage = game:GetService("ServerStorage")
local Pathfinding = game:GetService("PathfindingService")
local NPCStorage = Storage.NPCStorage
local NPCWorkspace = game.Workspace.NPCs
local Interactives = NPCWorkspace.Interactives
local Spawned = NPCWorkspace.Spawned
local NameList = {"Aurora", "Caleb", "Luna", "Phoenix", "Serenity", "Xander", "Nova", "Elena", "Kai", "Amara", "Dexter",
	"Zara", "Axel", "Aria", "Zephyr", "Selena", "Orion", "Lyra", "Cyrus", "Violet"}

while true do
	local TempData = false

	for _, Seat in pairs(Interactives.Seats:GetChildren()) do
		if Seat:IsA("Seat") then
			if Seat:FindFirstChild("Occupied") then
				local TempValue = Seat.Occupied
				if TempValue.Value == false then
					TempData = true
					break
				end
			end
		end
	end

	if TempData == true then
		local TempStorage = NPCStorage:GetChildren()
		local PathStorage = Interactives.Paths

		if #TempStorage > 0 then
			local RandomIndex = math.random(1, #TempStorage)
			local NPCSpawned = TempStorage[RandomIndex]
			local ClonedNPC = NPCSpawned:Clone()
			ClonedNPC.PrimaryPart = ClonedNPC.HumanoidRootPart
			ClonedNPC.Parent = Spawned
			ClonedNPC.Name = math.random(1, #NameList)
			local Humanoid = ClonedNPC:FindFirstChildOfClass("Humanoid")

			if Humanoid then
				local SelectedPath = math.random(1, 2)
				local NewPath = PathStorage:FindFirstChild("Path" .. SelectedPath)

				if NewPath then
					local path = Pathfinding:CreatePath({
						AgentRadius = 3,
						AgentHeight = 6,
						AgentCanJump = false,
					})

					local waypoints = {}
					for _, Attachment in pairs(NewPath:GetChildren()) do
						if Attachment:IsA("Attachment") then
							table.insert(waypoints, Attachment.Position)
						end
					end

					path:ComputeAsync(ClonedNPC.PrimaryPart.Position, waypoints)

					path.Blocked:Connect(function()
						print("Path blocked!")
					end)

					path.PathFound:Connect(function()
						print("Path found!")
					end)

					path.Completed:Connect(function()
						print("Path completed!")
					end)

					Humanoid:MoveTo(waypoints[#waypoints])

				end
			end
		end
	end
	task.wait(5)
end
1 Like

The problem was resolved.

local Storage = game:GetService("ServerStorage")
local Pathfinding = game:GetService("PathfindingService")
local NPCStorage = Storage.NPCStorage
local NPCWorkspace = game.Workspace.NPCs
local Interactives = NPCWorkspace.Interactives
local Spawned = NPCWorkspace.Spawned
local SelectedPath = script.SelectedValue
local NameList = {"Aurora", "Caleb", "Luna", "Phoenix", "Serenity", "Xander", "Nova", "Elena", "Kai", "Amara", "Dexter",
	"Zara", "Axel", "Aria", "Zephyr", "Selena", "Orion", "Lyra", "Cyrus", "Violet"}

while true do
	local TempData = false

	for _, Seat in pairs(Interactives.Seats:GetChildren()) do
		if Seat:IsA("Seat") then
			if Seat:FindFirstChild("Occupied") then
				local TempValue = Seat.Occupied
				if TempValue.Value == false then
					TempData = true
					break
				end
			end
		end
	end

	if TempData == true then
		local TempStorage = NPCStorage:GetChildren()
		local PathStorage = Interactives.Paths

		if #TempStorage > 0 then
			local RandomIndex = math.random(1, #TempStorage)
			local NPCSpawned = TempStorage[RandomIndex]
			local ClonedNPC = NPCSpawned:Clone()
			ClonedNPC.PrimaryPart = ClonedNPC.HumanoidRootPart
			ClonedNPC.Parent = Spawned
			ClonedNPC.Name = math.random(1, #NameList)
			local Humanoid = ClonedNPC:FindFirstChildOfClass("Humanoid")

			if Humanoid then
				local NewPath = PathStorage:FindFirstChild("Path" .. SelectedPath.Value)
				
				print(SelectedPath.Value)

				if NewPath then
					local path = Pathfinding:CreatePath({
						AgentRadius = 0,
						AgentHeight = 10,
						AgentCanJump = false,
					})

					local waypoints = {}
					
					for i = 1, #NewPath:GetChildren() do
						table.insert(waypoints, NewPath[tostring(i)].Position)
					end

					path:ComputeAsync(ClonedNPC.PrimaryPart.Position, waypoints[1])
					Humanoid:MoveTo(waypoints[1])
					Humanoid.MoveToFinished:Wait()
					
					path:ComputeAsync(ClonedNPC.PrimaryPart.Position, waypoints[2])
					Humanoid:MoveTo(waypoints[2])
					Humanoid.MoveToFinished:Wait()
					
					path:ComputeAsync(ClonedNPC.PrimaryPart.Position, waypoints[3])
					Humanoid:MoveTo(waypoints[3])
					Humanoid.MoveToFinished:Wait()
					
					path:ComputeAsync(ClonedNPC.PrimaryPart.Position, waypoints[4])
					Humanoid:MoveTo(waypoints[4])
					Humanoid.MoveToFinished:Wait()
					
					path:ComputeAsync(ClonedNPC.PrimaryPart.Position, waypoints[5])
					Humanoid:MoveTo(waypoints[5])
					Humanoid.MoveToFinished:Wait()
				end
			end
		end
	end
	task.wait(1)
end
1 Like

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