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