Hello, I'm having a weird issue where my NPCs stop moving automatically even though there in a loop as seen here: https://gyazo.com/fee3e0c2f012e796487a128a5b007243
here is my movement script for the NPC:
local atkDistance = 4
local eTower = script.Parent:WaitForChild("eTower")
local Tower = workspace.Game:FindFirstChild(eTower.Value)
--/// myNPC
local NPC = script.Parent.Parent
local HRP = NPC:WaitForChild("HumanoidRootPart")
local Hum = NPC:WaitForChild("Humanoid")
local AI = NPC:WaitForChild("AI")
local Team = AI:WaitForChild("Team")
local Target = AI:WaitForChild("Target")
local moveToTower = AI:WaitForChild("TouchTower")
--/// Event
local Event = script.Parent:WaitForChild("Trigger")
local findTarget = script.Parent:WaitForChild("findTarget")
Hum:MoveTo(Tower.Target.Position)
Target.Changed:Connect(function()
--/// Enemy NPC
local eNPC = workspace.Game.npcStorage:FindFirstChild(Target.Value)
local eHRP = eNPC:FindFirstChild("HumanoidRootPart")
while true do wait(.1)
if Target.Value == "" or eNPC:FindFirstChild("Humanoid").Health <= 0 then
Target.Value = ""
Hum:MoveTo(Tower.Target.Position)
--/// Attack or Move
elseif (HRP.Position - eHRP.Position).magnitude <atkDistance then -- Checks if the distance between the enemy and NPC is less than 5 studs
Event:FireServer()
elseif Target.Value ~= nil then
Hum:MoveTo(eHRP.Position)
--Hum.MoveToFinished:Wait() -- May improve srv performance but will make NPCs appear derpy as they may walk to far than suppose to
end
end
end)
My NPC is programmed to chase down the nearest Enemy yet in studio it works fine-ish (Can still freeze but moves alot further than when im playing in real time.) If anyone has a solution please share it as i'd love to get this fixed as it will cause a big issue for my game if unsolved. Thanks
EDIT: I am also receiving an error from where it says "if Target.Value == "" or eNPC:FindFirstChild("Humanoid").Health <= 0 then" saying "attempt to index a nil value" This error only pops up once the NPCs have killed an enemy and ends up spamming my output bar. I also do not know how to counter this situation.

3 comments
You've got rid of some errors but now i've got one saying "attempt to index local 'eNPC' (a nil value)"
this is on your 'line 1'
When the NPC is killed I'm assuming it's destroyed in which cause you wouldn't even be able to find eNPC. So you need to do 2 things. One, make sure eNPC is not nil, and two make sure you redefine the eNPC to the current target.