Changing target for NPC

I’m trying to get my NPCs to target a specific person on your command, and change that target when you tell them to, even if they’re trying to get to the old target.
The problem I’m facing is that the NPCs are trapped in a loop where they run after the old target and the new target at the same time, when I want them to “forget” the old target and solely focus on the new one.

Here’s a demo of what happens:

The module script controlling all the NPCs is as follows:

function MindlessTitanController:RunAI(titan,configs)
	while wait(updateTime) and titan.Humanoid.Health > 0 do
		local target, targetX = getClosestVisibleChar(titan)

		if RS.Events.CoordinateTarget.Event:Connect(function(target)
				
				if target ~= titan then
					Coordinating = true
					while wait(updateTime) and titan.Humanoid.Health > 0 and Coordinating == true do
						if targetX < configs.AggroDistance*1.8 then
							spawn(function()
								orientTitan(titan,target)
								configs.CoordinateTitan(target)
							end)
						end

	if target.Humanoid.Died:Connect(function()
								Coordinating = false
							end)then
						end
					end
				end
			end)then
		end
		end
	end
end

In another script inside the NPC:

configs.CoordinateTitan = function(target)
	hum.WalkSpeed = math.random(WalkSpeed*(10^(1/mag)),WalkSpeed*(16^(1/mag)))
	if walkTrack.IsPlaying then
		walkTrack:Stop()
	end
	if not runTrack.IsPlaying then
		runTrack:Play()
	end
	hum:MoveTo(target.HumanoidRootPart.Position)
end

What I expect to happen is for the “target” to update, and completely forget about the old target. However, it doesn’t do that as it’s very clearly trying to go to both at the same time. I believe it’s running the same while loop because no condition has changed to stop it.

If there are any ideas on how I could stop this weird bug, it would be much appreciated

2 Likes

After a while of tinkering around with the script, I managed to find a reliable solution. I used spawn() to create a seperate function, which apparently updates the target, and stops the NPCs pursuit of the previous target:

function MindlessTitanController:RunAI(titan,configs)
	while wait(updateTime) and titan.Humanoid.Health > 0 do
		local target, targetX = getClosestVisibleChar(titan)

		if RS.Events.CoordinateTarget.Event:Connect(function(t,action)
				if t ~= titan then
					Coordinating = true
					local hand = titan.RightHand or titan.LeftHand
					configs.StandStill(titan)
					wait()
					orientTitan(titan,t)
					wait(1.5)
					spawn(function()
						while wait(updateTime) and t and titan.Humanoid.Health > 0 and Coordinating == true  do
							if Coordinating == false then break end
								orientTitan(titan,t)
								configs.CoordinateTitan(titan, t)

							if t.Humanoid.Died:Connect(function()
									Coordinating = false
									configs.StandStill(titan)
									t = nil
								end)then
							end
							if Coordinating == false or titan.Humanoid.Died:Connect(function()
									Coordinating = false
									configs.StandStill(titan)
								end)then
							end
						end
					end)
				end
			end)then
		end
	end
end

If anyone has any better solutions, by all means provide any. Otherwise, this is the only solution I’ve found thus far.