local npc = script.Parent local head = npc:FindFirstChild("Head") local debris = game:GetService("Debris") local screamSound = head:FindFirstChild("ScreamSound") local Zone = require(game.ReplicatedStorage.Zone) local playersInZone = require(game.ServerScriptService.PlayersInZone3Script) local container = workspace:FindFirstChild("RegionPart3") local zone = Zone.new(container) local npcTorso = npc:FindFirstChild("Torso") local pathfindingService = game:GetService("PathfindingService") local quickSlash = npc.Swing local quickSlashAnimTrack = npc.Humanoid:LoadAnimation(quickSlash) quickSlashAnimTrack.Priority = Enum.AnimationPriority.Action local properties = {["WaypointSpacing"] = 1.1, ["AgentRadius"] = 2.5 } local runService = game:GetService("RunService") function getClosestPlayer() local closestPlayer, closestDistance = nil, 200 for i,char in pairs(workspace:GetChildren()) do if char:FindFirstChild("Humanoid") and char ~= npc then local hmr = char:FindFirstChild("HumanoidRootPart") if hmr then local distance = (npcTorso.Position - char.HumanoidRootPart.Position).Magnitude if distance < closestDistance then closestPlayer = char closestDistance = distance end end end end return closestPlayer, closestDistance end function moveNPC() local path = pathfindingService:CreatePath(properties) local char, distance = getClosestPlayer() if char then path:ComputeAsync(npc.HumanoidRootPart.Position,char.PrimaryPart.Position) if (npc.HumanoidRootPart.Position - char.HumanoidRootPart.Position).Magnitude >20 and (npc.HumanoidRootPart.Position - char.HumanoidRootPart.Position).Magnitude >22 then screamSound:Play() end local waypoints = path:GetWaypoints() for i, waypoint in pairs(waypoints) do local part = Instance.new("Part") part.Size = Vector3.new(1,1,1) part.Transparency = 0.7 part.CanCollide = false part.Position = waypoint.Position part.Parent = workspace debris:AddItem(part,.5) if (npc.HumanoidRootPart.Position - char.HumanoidRootPart.Position).Magnitude > 2.2 then npc.Humanoid:MoveTo(waypoint.Position) end while true do local distance = (npc.HumanoidRootPart.Position - waypoint.Position).Magnitude if distance < 5 then break end wait() end end end end --[[zone.playerEntered:Connect(function(player) table.insert(playersInZone,player) print(player.Name.." entered zone 3") end) zone.playerExited:Connect(function(player) local playerFound = table.find(playersInZone,player) wait(0.5) table.remove(playersInZone,player) print(player.Name.." exited zone 3") end)--]] while wait() do --for i,player in pairs(playersInZone) do -- if i == 1 then runService.Stepped:Connect(moveNPC) --end --end end
https://i.gyazo.com/cf2e7eca61d5f5f6f99e6ec9d615e889.mp4
So the NPC is walking towards the closest humanoid in a maze, however he seems to be bugging out (refer to gyazo link). I notice this only when I use runService.Stepped. If I just use a while loop, it doesn't happen however it doesn't follow the character smoothly, so I want to use runService. It seems to be moving back on itself, like as it it's trying to move to a previous waypoint for a split second. Anyway, if anyone can help I'd appreciate