I want to stop a npc mid pathfinding but how would I do that?
Code:
local GameInProgress = game.ServerStorage.GameValues:WaitForChild("GameInProgress")
local CollectionService = game:GetService("CollectionService")
local PathFindingService = game:GetService("PathfindingService")
local NpcsCollection = CollectionService:GetTagged("Npcs")
local path = PathFindingService:CreatePath()
local function setupNpcs(Npcs)
GameInProgress.Changed:Connect(function()
if GameInProgress.Value == true then
task.wait(3)
repeat
local RandomObj = Random.new()
local Availablewaypoints = workspace.School.Npcwaypoints:GetChildren()
local ChoosenWaypoint = Availablewaypoints[RandomObj:NextInteger(1,#Availablewaypoints)]
task.wait(0.2)
local function getpath(destination)
local pathparams = {
["AgentHeight"] = 5.37,
["AgentRadius"] = 2,
["AgentCanJump"] = true,
}
local path = PathFindingService:CreatePath(pathparams)
if GameInProgress.Value == true then
path:ComputeAsync(Npcs.HumanoidRootPart.Position, destination.Position)
end
return path
end
local function walkTo(destination)
local path = getpath(destination)
for index, waypoint in pairs(path:GetWaypoints()) do
Npcs.Humanoid:MoveTo(waypoint.Position)
path.Blocked:Connect(function()
local path = getpath(destination)
walkTo(ChoosenWaypoint)
end)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Npcs.Humanoid.Jump = true
end
Npcs.Humanoid.MoveToFinished:Wait()
end
end
if Npcs:FindFirstChild("Occupied") then
if Npcs:FindFirstChild("Occupied").Value == false then
print(Npcs.Name)
walkTo(ChoosenWaypoint)
end
end
Npcs:FindFirstChild("Occupied").Changed:Connect(function()
if Npcs.Occupied.Value == true then
print(Npcs.Name)
walkTo(Npcs.HumanoidRootPart)
end
end)
until GameInProgress.Value == false
end
end)
end
local function NpcAdded(Npcs)
setupNpcs(Npcs)
end
-- Connect existing Npcs
for _, model in pairs(NpcsCollection) do
setupNpcs(model)
end
-- Connect new Npcs
CollectionService:GetInstanceAddedSignal("Npcs"):Connect(NpcAdded)