I have a script that involves several NPCs traversing paths around the map by moving to different pre-set waypoint parts.
The green parts (normally transparent) are the waypoints that the NPCs move to. Each one of these waypoints is the parent of multiple object values of the adjacent waypoints that are available to move to.
Normally the NPCs walk continuously from waypoint to waypoint and have the chance to take a small break from walking when they reach their destination, but the issue is that occasionally they will go to a full stop of all movement at stay at a waypoint forever.
The NPCs all walk from point to point for awhile but eventually stop in their tracks with the exception of “he who never stops.” Here is my script:
NPCFolder = script.Parent
Characters = NPCFolder.Characters
Waypoints = NPCFolder.Paths.Waypoints
-- Function to move NPC without Timeout
function moveTo(humanoid, targetPoint, CurrentWaypoint)
local targetReached = false
if humanoid.Parent:FindFirstChild("Waiting") then
humanoid.Parent.Waiting:Destroy()
end
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if humanoid.Parent.Name ~= "him" then -- This if-statement is here to make it so occasinally NPCs will stop in their tracks and consider life choices (unless they are him who never stops)
local WaitTag = Instance.new("BoolValue", humanoid.Parent) -- add a tag to the player to show they are waiting (for debugging purposes)
WaitTag.Name = "Waiting"
local rand = math.random(0, 3) -- set a random number to determine if the NPC should wait first
if rand == 0 then
wait(math.random(2,7))
end
end
-- Choose the next waypoint from the availible adjacent paths
local AvailibleWaypoints = CurrentWaypoint.Value:GetChildren()
local NextWaypoint = AvailibleWaypoints[math.random(1, #AvailibleWaypoints)]
-- Recurse the function
moveTo(humanoid, NextWaypoint.Value.Position, CurrentWaypoint)
CurrentWaypoint.Value = NextWaypoint.Value
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
-- Main Moving Loop
for _,NPC in pairs(Characters:GetChildren()) do
local CurrentWaypoint = Instance.new("ObjectValue", NPC)
CurrentWaypoint.Name = "CurrentWaypoint"
-- Move the NPC to an initial starting position
local WaypointsTable = Waypoints:GetChildren()
local StartingWaypoint = WaypointsTable[math.random(1, #WaypointsTable)]
NPC.PrimaryPart.CFrame = CFrame.new(StartingWaypoint.Position) + Vector3.new(0, 10, 0)
CurrentWaypoint.Value = StartingWaypoint
-- Choose the next availible path to an adjacent waypoint and being moving
local AvailibleWaypoints = CurrentWaypoint.Value:GetChildren()
local NextWaypoint = AvailibleWaypoints[math.random(1, #AvailibleWaypoints)]
moveTo(NPC.Humanoid, NextWaypoint.Value.Position, CurrentWaypoint)
CurrentWaypoint.Value = NextWaypoint.Value
end
At this point I am going to go into some speculation of what I think the problem is. The only difference between “He who never stops” and the rest of the NPCs is walkspeed. He runs frighteningly fast at light speed in contrast to the other NPCs who slowly walk around the map. This could potentially be why he never stops because his initial “Humanoid:MoveTo()” is never given the full 8 seconds to time out and he always reaches his destination. The other NPCs might be bugging out because their timeout is resetting and that section of the code is buggy?
The other interesting part is none of the NPCs who are broken and stopped in their tracks forever have the “Waiting” tag, so they are continuously trying to move to the next waypoint but never getting anywhere.
This has stumped me for the past few days so if you guys can help that would be excellent.
enjoy this image of my poorly made roblox steph curry