local NPCs = game.ServerStorage["NPC's"]:GetChildren()
local function Move(character, Position)
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {Water = 20}
})
local humanoid = character:WaitForChild("Humanoid")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local function followPath(destination)
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.HumanoidRootPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
-- Detect if path becomes blocked
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting path blockage until path is re-computed
blockedConnection:Disconnect()
-- Call function to re-compute new path
followPath(destination)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
-- Initially move to second waypoint (first waypoint is path start; skip it)
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
followPath(Position)
end
local function createNPC(position, parent)
local chosenNPC = NPCs[math.random(1, #NPCs)]
print(chosenNPC)
local bot = chosenNPC:Clone()--game.ServerStorage["NPC's"]:FindFirstChild(chosenNPC):Clone()
print(bot.Name)
bot.Parent = parent
bot:PivotTo(CFrame.new(position))
Move(bot, Vector3.new(200, 0, 200))
end
while wait(math.random(1,5)) do
createNPC(game.Workspace.Tycoons.bostnmTycoon.House.Plot.Position + Vector3.new(0, 5, 0), game.Workspace.Tycoons.bostnmTycoon.NPCs)
end
I need help finding what is wrong with this code. When I test it it gives a warning saying
Path not computed! nil
I believe it goes wrong on the part where it says
path:ComputeAsync(character.HumanoidRootPart.Position, destination)
Although I don’t know what I’m doing wrong as my NPC model looks like this:
Any ideas on why my code might not work?
Ignore the comments
