Exhausted allowed execution time for finding magnitude

This is a compound script on server script storage for controlling a bunch of R6 zombie whose humanoid is named “Zombie”.
When the findNearestTorso function is ran, there will be an error at this line:

dist = (RootPart.Position - pos).Magnitude

This is my problem. When this line is reached, it occasionally work and usually gives this error:

ServerScriptService.Game.MonsterHandler:114: Script timeout: exhausted allowed execution time

local pf = game:GetService("PathfindingService")

workspace.DescendantAdded:Connect(function(descendant)
	if collector:HasTag(descendant,"AI") then
		spawn(function()
			local OwnRoot = descendant:FindFirstChild("HumanoidRootPart")
			local attack = true
			
			local damage = math.random(5,10)
			local SightRange = 10000
			
			local target
			local Droid = descendant:WaitForChild("Zombie")
			local OriginalSpeed = Droid.WalkSpeed
			
			local Sound = Instance.new("Sound",descendant.Head)
			Sound.Name = "ZombieSound"
			local Soundlist = script:GetChildren()
			
			spawn(function()
				while wait(math.random(1,5))do
					Sound.SoundId = Soundlist[math.random(1,#Soundlist)].SoundId
					Sound.PlaybackSpeed = math.random(0.3,0.7)
					Sound:Play()
					wait(Sound.TimeLength)
					
					if descendant == nil then
						break
					end
				end
			end)
			
			local function findNearestTorso(pos)
				local list = workspace:GetChildren()
				local torso = nil
				local dist = SightRange
				local RootPart = nil
				local human = nil
				local CurrentModel = nil
				for x = 1, #list do
					CurrentModel = list[x]
					if CurrentModel and (CurrentModel:IsA("Model")) and (CurrentModel ~= descendant) then
						RootPart = CurrentModel:FindFirstChild("HumanoidRootPart")
						human = CurrentModel:FindFirstChild("Humanoid")
						if (RootPart ~= nil) and (human ~= nil) and (human.Health > 0) then
							local d = (RootPart.Position - pos).magnitude
							if d < dist then
								torso = RootPart
								dist = (RootPart.Position - pos).Magnitude
							end
						end
					end
				end
				
				return torso
			end
			
			Droid.Died:Connect(function()

				local tag = Droid:FindFirstChild("creator")
				if tag and tag:IsA("ObjectValue") then
					local player = tag.Value
					local statss = player:FindFirstChild("leaderstats")
					
					if statss then
						local Credits = statss:FindFirstChild("Credits")
						if Credits and Credits:IsA("IntValue") then
							Credits.Value = Credits.Value + 10
						end
						
					end
				end
				
				local sound = descendant.Head:FindFirstChild("Death")
				if sound and sound:IsA("Sound") then
					sound.PlaybackSpeed = math.random(0.5,2)
					sound:Play()
				end
				
				wait(2)
				
				for i,v in pairs(descendant:GetChildren())do
					if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
						v.Anchored = true
						v.CanCollide = false
					end
				end
			end)
			
			Droid.HealthChanged:Connect(function()
				if descendant:FindFirstChild("Zombie") then
					descendant.Zombie.WalkSpeed = OriginalSpeed * (descendant.Zombie.Health / descendant.Zombie.MaxHealth)
					if descendant.Zombie.WalkSpeed < OriginalSpeed/1.5 then
						descendant.Zombie.WalkSpeed = OriginalSpeed/1.5
					end
				end
			end)
			
			descendant["Torso"].Touched:Connect(function(hit)
				if hit and attack == true then
					local Human = hit.Parent:FindFirstChild("Humanoid")
					if Human then
						Human:TakeDamage(damage)
						attack = false
						wait(0.5)
						attack = true
					end
				end
			end)
			
			while true do
				
				target = findNearestTorso(OwnRoot.Position)
				if target ~= nil then
					
					local p = pf:CreatePath({
						AgentRadius = 2,
						AgentHeight = 5,
						AgentCanJump = true
					})
							
					local pos = target.Position
					p:ComputeAsync(OwnRoot.Position, pos)
							
					if p.Status == Enum.PathStatus.Success then
						
						local LST = p:GetWaypoints()
						
						if LST then
							
							for i,v in pairs(LST) do
								
								local WP = LST[i]
								if WP.Action == Enum.PathWaypointAction.Jump then
									Droid.Jump = true
								end
								
								Droid:MoveTo(WP.Position)
								Droid.MoveToFinished:Wait(0)
								
								if (target.Position - pos).Magnitude > 20 then
									break
								end
							end
						end
					end
				end
			end
		end)
	end
end)