I can't break a loop for some reason

Hello, I am trying to get an NPC to move to my mouse location using pathfinding, but I need to be able to adjust his ending location.

  1. I am trying to break a loop if the mouse is clicked a second time.

  2. It registers the loop breaking (via the print()), but the loop keeps playing.

  3. I have tried multiple misc things, but the loop keeps playing.

	if isMoving then
		secondClick = true
	end
	
	local path = pathService:CreatePath(pathParams)
	
	path:ComputeAsync(soldier.Position, pos)
	
	local waypoints = path:GetWaypoints()
	
	workspace.PathFindingObjects:ClearAllChildren()
	
	for _, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.Shape = "Ball"
		part.Material = "Neon"
		part.Size = Vector3.new(0.6, 0.6, 0.6)
		part.Position = waypoint.Position
		part.Anchored = true
		part.CanCollide = false
		part.Parent = workspace.PathFindingObjects
	end
	
	isMoving = true
	
	for _, waypoint in pairs(waypoints) do
		soldier.Parent.Humanoid:MoveTo(waypoint.Position)
		soldier.Parent.Humanoid.MoveToFinished:Wait()
		
		if secondClick then
			print("loop broken")
			secondClick = false
			break
		end
	end
	
	print("path ended")
	
	isMoving = false

Thank you in advance!

if secondClick then

is an abbreviation of

if secondClick == true then
1 Like

I would probably alter the approach. Instead of giving an NPC a command and then telling the NPC to do the command. Have the action and the command separately. Let me explain.

  1. When user clicks set the destination of the NPC to the click.

Then separately keep checking.

  1. If the NPC is not at the destination then finish(return)
  2. Otherwise, create a path from the Current Position to the destination.-- You can see this is separate from the command
  3. Then follow the first waypoint of that path.
  4. Go back to step 1

What I mean is that I want the NPC to stop its current path and use the new one.

Yes the looping handles that. That is exactly why I would separate the movement from the commands. Otherwise the NPC will be forced to follow the command until the end.

I am not really good at explaining. Please try to read the post carefully.

Thank you. I believe it will work, but I’m currently implementing it.

I’m not too familiar with something like this but it outputs the final print as if it “breaks” but is still in the for loop aswell.

soldier.Parent.Humanoid:MoveTo(waypoint.Position)
soldier.Parent.Humanoid.MoveToFinished:Wait()

perhaps try to have the above under a condition secondclick==false?

i really don’t know so take with grain of salt

It worked! The movement is a bit choppy, but I can fix that easily.

RE.OnServerEvent:Connect(function(plr, pos)	
	destination = pos
	atDestination = false
end)

while true do
	if not atDestination then
		print("Moving to destination")
		
		local path = pathService:CreatePath(pathParams)

		path:ComputeAsync(soldier.Position, destination)

		local waypoints = path:GetWaypoints()

		soldier.Parent.Humanoid:MoveTo(waypoints[2].Position)
		soldier.Parent.Humanoid.MoveToFinished:Wait()
		
		if waypoints[2].Position == destination then
			atDestination = true
		end
	end
	
	wait()
end
1 Like