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