While loops stops without fullfilling the conditions

The while loop that I wrote for a zombie AI stops once it gets in range of 5 studs with the player Character.
It has two conditions:
One is wait ( )
One is decendant(basically the zombie itself)
But without the zombie being destroyed, the loop stops by itself.

Here is what the code looks like:

    while descendant and wait(0.1) do
		if debug == true then
			number = number + 1
			print(number)
		end
			
		local target = findNearestTorso(OwnRoot.Position)
			
		if target then
				
			local p = pf:CreatePath()
			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 = 1,5 do
							
						local WayPoint = LST[i]
							
						if LST[i].Action == Enum.PathWaypointAction.Jump then
							Droid.Jump = true
						end
							
						if debug == true then
							local Part = Instance.new("Part")
							Part.CanCollide = false
							Part.Material = Enum.Material.Neon
							Part.Size = Vector3.new(.1,.1,.1)
							Part.Anchored = true
								
							Part.CFrame = CFrame.new(WayPoint.Position)
							Part.Parent = workspace
							game.Debris:AddItem(Part,5)
						end
							
						Droid:Move(WayPoint.Position - OwnRoot.Position)

					end
				end
					
					
			end
		
		end
			
			
	end

So right there is no

break or return

in this extract.

Droid is the zombie’s Humanoid and findNearestTorso returns you a HumanoidRootPart or nil.

For the debug part, basically stops generating new path indicators (Neon blocks) after it goes near the player.

For every run of the loop, the number which indicates how many times that the loop has run, will be printed.
It stops printing after they go near each other (player and zomb)

This really bothers me as a lot of my players have gave me the description of this bug but I am just not capable of soving it.

Upmost appreciation to any helping hands.

PS: loops are not being breaked properly 6 hrs after this post.

a few questions upfront first . . why is there a wait(0.1) in your while condition and also why are your waypoints limited to max 5? ( if I am reading correctly)

sometimes the waypoints can go up to tens and hundred if the path is long . .

I purposely made it like that so the path will have a shorter range and less error in case the player moves by a lot before the zombie reaches him. The wait is to decrease chances of that the loop is extremely fast and breaks the system.

Please tell me what the descendant variable is on line 1.

1 Like

it is a R6 character for which is being cloned into the workspace by the server