It would be very helpful to show the whole script, if possible
I mean, it’s definitely possible to have delays within loops
@6Communist_Wolf9
You can use this script for your enemy, if you want:
-- Tested using a server script that's a direct child of the enemy NPC
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local DETECT_RADIUS = 32 -- Farthest distance that the enemy will be able to detect players. In studs
local enemy = script.Parent
local enemyRootPart = enemy.PrimaryPart or enemy:WaitForChild("HumanoidRootPart")
local humanoid = enemy:WaitForChild("Humanoid"):: Humanoid
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false}) -- Jumping support will require modifying the code a bit
local function getClosestPlayer(): (Player?, Vector3?)
local closestPlayer, playerPosition = nil, nil
local distanceFromPlayer = math.huge
for _, player in Players:GetPlayers() do
local safe = player:FindFirstChild("Safe"):: BoolValue?
if safe and safe.Value == true then continue end -- This will skip this player if they're marked as safe
local character = player.Character
if character == nil then continue end -- This will skip the player if they don't have a character
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.Health <= 0 then continue end -- This will skip the player if they're already dead
local rootPart = character.PrimaryPart
local distance = rootPart and (rootPart.Position - enemyRootPart.Position).Magnitude
if distance and distance <= DETECT_RADIUS and distance < distanceFromPlayer then
closestPlayer = player
playerPosition = rootPart.Position
distanceFromPlayer = distance
end
end
return closestPlayer, playerPosition
end
enemyRootPart:SetNetworkOwner(nil) -- This will prevent players from being able to teleport the enemy...
-- ...if they automatically become the network owner
while true do
local closestPlayer, playerPosition = getClosestPlayer()
if closestPlayer then
path:ComputeAsync(enemyRootPart.Position, playerPosition)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in path:GetWaypoints() do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
task.wait()
end -- For future reference, code written below this line won't run, as it will be blocked by the loop
I tested it using a server script that’s parented to one of the rigs generated by the rig builder tool
@6Communist_Wolf9
I’ve made some changes to the script above, so that it will skip players that are marked as safe, or are already dead. It should work the same, or at least very close, to the script you’re currently using 

Original version of my script, before adding the changes mentioned above
-- Tested using a server script that's a direct child of the enemy NPC
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local DETECT_RADIUS = 32 -- Farthest distance that the enemy will be able to detect players. In studs
local enemy = script.Parent
local enemyRootPart = enemy.PrimaryPart or enemy:WaitForChild("HumanoidRootPart")
local humanoid = enemy:WaitForChild("Humanoid"):: Humanoid
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false}) -- Jumping support will require modifying the code a bit
local function getClosestPlayer(): (Player?, Vector3?)
local closestPlayer, playerPosition = nil, nil
local distanceFromPlayer = math.huge
for _, player in Players:GetPlayers() do
local rootPart = player.Character and player.Character.PrimaryPart
local distance = rootPart and (rootPart.Position - enemyRootPart.Position).Magnitude
if distance and distance <= DETECT_RADIUS and distance < distanceFromPlayer then
closestPlayer = player
playerPosition = rootPart.Position
distanceFromPlayer = distance
end
end
return closestPlayer, playerPosition
end
enemyRootPart:SetNetworkOwner(nil) -- This will prevent players from being able to teleport the enemy...
-- ...if they automatically become the network owner
while true do
local closestPlayer, playerPosition = getClosestPlayer()
if closestPlayer then
path:ComputeAsync(enemyRootPart.Position, playerPosition)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in path:GetWaypoints() do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
task.wait()
end -- For future reference, code written below this line won't run, as it will be blocked by the loop