I’m helping make a prison game that will have NPCs with schedules, I have a script updating the time and firing events at times when all NPCs should move. If an NPC has not made it to their goal before the next event is fired, they should cancel their route and begin pathfinding to the next point.
All NPCs have their own places they need to be at certain times and their schedules are attributes on their models.
Currently, if an NPC is unable to make it to their current destination before the next, instead of cancelling their current path, they go back and forth and eventually the paths seem to stack up and they go insane and run into walls.
If NPCs are able to make it to all their goals on time the script runs fine.
I’ve read through other posts on the forum talking about this issue. One suggested adding a thread check, using newproxy(). Another suggested a path cancel function to be called whenever the schedule event is fired. Neither of these methods worked for me.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PathfindingService = game:GetService("PathfindingService")
local function pathFindTo(char, destination)
local path = PathfindingService:ComputeRawPathAsync(char.HumanoidRootPart.Position, destination.Entrance.Position, math.huge)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(path:GetWaypoints()) do
if waypoints[i].Action == Enum.PathWaypointAction.Jump then
char:FindFirstChildWhichIsA("Humanoid").Jump = true
end
char:FindFirstChildWhichIsA("Humanoid"):MoveTo(waypoints[i].Position)
char:FindFirstChildWhichIsA("Humanoid").MoveToFinished:Wait()
end
end
for i, char in ipairs(workspace.Characters:GetChildren()) do
char.PrimaryPart:SetNetworkOwner(nil)
ReplicatedStorage.Events.UpdateSchedule.Event:Connect(function(eventTime)
if char:GetAttribute("At"..eventTime) == nil then return end
local destination = workspace.Prison:FindFirstChild(char:GetAttribute("At"..eventTime))
pathFindTo(char, destination)
end)
end
Being printed is the minutes after midnight - the intended path of the NPCs is to go back around the corner they came from.
Also let me know if I need to clean up my script or if I messed up anything stupid I’m new to scripting!!